I'm getting started with C programming. I currently have a large file that contains a lot of functions. I would like to move these functions to a separate file so that the code is easier to read. However, I can't seem to figure out how to properly include/compile and can't find an example in any online tutorials that I've found. Here's a simplified example:
#include <stdlib.h> #include <stdio.h> void func1(void) { printf("Function 1!\n"); } void func2(void) { printf("Function 2!\n"); } int main(void) { func1(); func2(); return 0; }
How do you move C functions into a separate file? FYI: I'm using gcc.
Update: These answers are very helpful, thank you. Now it seems that my simplified example is not good enough because I realized the reason my program failed to compile is because I'm using a global variable in my functions.
#include <stdlib.h> #include <stdio.h> int counter = 0; void func1(void) { printf("Function 1!\n"); counter++; } int main(void) { func1(); return 0; }
Moving these functions to an external file doesn't work because they need to reference this global variable:
#include <stdlib.h> #include <stdio.h> #include "functions.c" int counter = 0; int main(void) { func1(); counter = 100; return 0; }
How can I get around this issue?
You can properly include . C or . CPP files into other source files.
Okay. Here we go.
Your main.c file
#include <stdlib.h> #include <stdio.h> #include "functions.h" int main(void) { func1(); func2(); return 0; }
Your functions.h file
void func1(void); void func2(void);
Your functions.c file
#include "functions.h" void func1(void) { printf("Function 1!\n"); } void func2(void) { printf("Function 2!\n"); }
Compile it with:
gcc -o main.exe main.c functions.c
The most common way is to place your function prototypes in a header file and your function implementations in a source file. For example:
func1.h
#ifndef MY_FUNC1_H #define MY_FUNC1_H #include <stdio.h> // declares a variable extern int var1; // declares a function void func1(void); #endif
func1.c
#include "func1.h" // defines a variable int var1 = 512; // defines a function void func1(void) { printf("Function 1!\n"); }
func2.h:
#ifndef MY_FUNC2_H #define MY_FUNC2_H #include <stdio.h> void func2(void); #endif
func2.c:
#include "func1.h" // included in order to use var1 #include "func2.h" void func2(void) { printf("Function 2 with var1 == %i\n", var1); }
main.c:
#include <stdio.h> #include "func1.h" #include "func2.h" int main(void) { var1 += 512; func1(); func2(); return 0; }
You would then compile using the following:
gcc -c -o func1.o func1.c gcc -c -o func2.o func2.c gcc -c -o main.o main.c gcc -o myprog main.o func1.o func2.o ./myprog
I only placed one function in each source/header pair for illustration. You could create just one header which includes the prototypes for all of the source files, or you could create multiple header files for each source file. The key is that any source file which will call the function, needs to include a header file which includes the function's prototype.
As a general rule, you only want a header file included once, this is the purpose of the #ifndef #define #endif
macros in the header files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With