How to move C functions to a separate file?

I start by programming in C. Currently, I have a large file containing many functions. I would like to move these functions to a separate file so that the code is easier to read. However, I cannot figure out how to properly enable / compile and cannot find an example in any online tutorial that I 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 to a separate file? FYI: I am using gcc.

Update:. These answers are very helpful, thanks. Now it seems that my simplified example is not good enough, because I understood the reason why my program could not be compiled, because I use 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 does not work, because they must 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 problem?

+12
source share
5 answers

Good. Like this.

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 this with:

 gcc -o main.exe main.c functions.c 
+22
source

The most common way is to place function prototypes in the header file and implement your function in the source file. For instance:

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; } 

Then you 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 just put one function in each source / header pair for illustration. You can create only one header that includes prototypes for all source files, or you can create multiple header files for each source file. The key is that any source file that will call the function must include a header file that includes the function prototype.

Typically, you want the header file to be included once, this is the purpose of the #ifndef #define #endif macros in the header files.

+8
source

First you need to know the difference between a declaration and a definition. The declaration tells the compiler that there is something like a function. Definition - for the case of functions - the implementation of the actual function.

So what do you do, move the definition to another file, but add the declaration to the file where the function should be called. Then you create both files together, and the compiler and linker take care of the rest.

+4
source

You can make two files: one with the main function, and the other with the rest.

If your function in another file uses a global variable from another module / file, you need to add "extern" before declaring the global variable in the module along with the function definition. This tells the compiler that you are accessing a global variable from another file.

The following is an example that defines the global variable "x" and calls the function "myfunction" in the file main.c. "Myfunction" is defined in the functions.c file. "X" also needs to be declared in the functions.c file, but since it will access "x" from another file, it must be "extern" before it. "myfunction" takes an external global variable "x", incrementing it by one and prints it. Consider the following code:

main.c will look something like this:

 #include <stdio.h> #include "functions.c" int x = 1; int main() { myfunction(); printf("The value of x in main is: %d\n", x); return(0); } 

functions.c will look something like this:

 #include <stdio.h> extern int x; void myfunction(void) { x++; printf("The value of x in myfunction is: %d\n",x); } 

Note that main.c uses "int x;" declare x, while functions.c uses "extern int x" to declare x.

To compile the code, make sure that both files are in the same folder, and compile as if you had only one file. For example, if you use gcc on linux and gcc, your command line input would look like this:

 gcc main.c -o main ./main 

The first line compiles the code, and the second line runs it. The output should look like this:

The x value in myfunction is: 2 The x value in main is: 2

Note that the value of "x" also changes basically, since "x" is a global variable.

-1
source

You can do something like this.

  /* func1.c */ void func1(void) { printf("Function 1!\n"); } /* func2.c */ void func2(void) { printf("Function 2!\n"); } /* main.c */ #include "func1.c" #include "func2.c" int main ( void ) { func1(); func2(); return 0; } 
-2
source

Source: https://habr.com/ru/post/957211/


All Articles