简体   繁体   中英

How to move C functions into separate file?

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?

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.

First you have to learn the difference between a declaration and definition . A declaration tells the compiler that something, like a function, exists. A definition is, for the case of functions, the actual function implementation.

So what you do is move the definition to another file, but add a declaration in the file where the function is to be called. You then build both files together, and the compiler and linker will take care of the rest.

You can make two files: one with the main function, and another with the remaining ones.

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

The following is an example where a global variable, "x", is defined and a function, "myfunction, is called in the main.c file. "myfunction" is defined in the functions.c file. "x" also needs to be declared in the functions.c file, but since it will be accessing "x" from another file, it needs to have an "extern" in front of it. "myfunction" takes the external global variable "x", increments it by one and prints it out. Consider the following code:

main.c will look somethin like:

#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:

#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;" to declare x whereas 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 just like you would if you only had one file. For example, if you use gcc on a linux system and gcc, your command line input will look like:

gcc main.c -o main
./main

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

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

Note that the Value of "x" is also changed in main as "x" is a global variable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM