如何将C函数移动到单独的文件中?

时间:2022-11-22 21:05:24

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:

我开始使用C编程了。我目前有一个包含很多功能的大文件。我想将这些函数移动到一个单独的文件,以便代码更容易阅读。但是,我似乎无法弄清楚如何正确包含/编译,并且无法在我找到的任何在线教程中找到示例。这是一个简化的例子:

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

如何将C函数移动到单独的文件中?仅供参考:我正在使用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?

我怎样才能解决这个问题?

5 个解决方案

#1


21  

Okay. Here we go.

好的。开始了。

Your main.c file

你的main.c文件

#include <stdlib.h>
#include <stdio.h>
#include "functions.h"

int main(void) {
  func1();
  func2();
  return 0;
}

Your functions.h file

你的functions.h文件

void func1(void);
void func2(void);

Your functions.c file

你的functions.c文件

#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

#2


7  

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.

作为一般规则,您只需要包含一次头文件,这是头文件中#ifndef #define #endif宏的用途。

#3


4  

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.

所以你要做的是将定义移动到另一个文件,但在要调用函数的文件中添加一个声明。然后,您将两个文件一起构建,编译器和链接器将负责其余的工作。

#4


-1  

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

#5


-1  

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

您可以创建两个文件:一个具有main函数,另一个具有其余文件。

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.

如果您在另一个文件中的函数使用来自另一个模块/文件的全局变量,则需要在模块中的全局变量声明前面添加“extern”和函数定义。这告诉编译器您正在从另一个文件访问全局变量。

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:

以下是定义全局变量“x”并在main.c文件中调用函数“myfunction”的示例。“myfunction”在functions.c文件中定义。“x”还需要在functions.c文件中声明,但由于它将从另一个文件访问“x”,它需要在它前面有一个“extern”。“myfunction”取外部全局变量“x”,递增它一个并打印出来。请考虑以下代码:

main.c will look somethin like:

main.c看起来像是:

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

functions.c将类似于:

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

请注意,main.c使用“int x;”声明x,而functions.c使用“extern int x”来声明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:

要编译代码,请确保两个文件都在同一个文件夹中,并且就像只有一个文件一样编译。例如,如果在linux系统和gcc上使用gcc,则命令行输入将如下所示:

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

myfunction中x的值为:2 main中x的值为:2

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

请注意,“x”的值也在main中更改,因为“x”是全局变量。

#1


21  

Okay. Here we go.

好的。开始了。

Your main.c file

你的main.c文件

#include <stdlib.h>
#include <stdio.h>
#include "functions.h"

int main(void) {
  func1();
  func2();
  return 0;
}

Your functions.h file

你的functions.h文件

void func1(void);
void func2(void);

Your functions.c file

你的functions.c文件

#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

#2


7  

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.

作为一般规则,您只需要包含一次头文件,这是头文件中#ifndef #define #endif宏的用途。

#3


4  

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.

所以你要做的是将定义移动到另一个文件,但在要调用函数的文件中添加一个声明。然后,您将两个文件一起构建,编译器和链接器将负责其余的工作。

#4


-1  

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

#5


-1  

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

您可以创建两个文件:一个具有main函数,另一个具有其余文件。

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.

如果您在另一个文件中的函数使用来自另一个模块/文件的全局变量,则需要在模块中的全局变量声明前面添加“extern”和函数定义。这告诉编译器您正在从另一个文件访问全局变量。

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:

以下是定义全局变量“x”并在main.c文件中调用函数“myfunction”的示例。“myfunction”在functions.c文件中定义。“x”还需要在functions.c文件中声明,但由于它将从另一个文件访问“x”,它需要在它前面有一个“extern”。“myfunction”取外部全局变量“x”,递增它一个并打印出来。请考虑以下代码:

main.c will look somethin like:

main.c看起来像是:

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

functions.c将类似于:

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

请注意,main.c使用“int x;”声明x,而functions.c使用“extern int x”来声明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:

要编译代码,请确保两个文件都在同一个文件夹中,并且就像只有一个文件一样编译。例如,如果在linux系统和gcc上使用gcc,则命令行输入将如下所示:

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

myfunction中x的值为:2 main中x的值为:2

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

请注意,“x”的值也在main中更改,因为“x”是全局变量。