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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19732959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:52:20  来源:igfitidea点击:

How to move C functions into separate file?

c

提问by Andrew

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?

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

回答by Raju Kunde

Okay. Here we go.

好的。开始了。

Your main.cfile

你的main.c文件

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

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

Your functions.hfile

你的functions.h文件

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

Your functions.cfile

你的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

回答by David M. Syzdek

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

函数1.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

功能1.c

#include "func1.h"

// defines a variable
int var1 = 512;

// defines a function
void func1(void) {
    printf("Function 1!\n");
}

func2.h:

func2.h:

#ifndef MY_FUNC2_H
#define MY_FUNC2_H
#include <stdio.h>
void func2(void);
#endif

func2.c:

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 #endifmacros in the header files.

作为一般规则,您只希望头文件包含一次,这是#ifndef #define #endif头文件中宏的目的。

回答by Some programmer dude

First you have to learn the difference between a declarationand 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.

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

回答by Aniq Ahsan

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:

下面是一个例子,在main.c文件中定义了一个全局变量“x”,一个函数“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”是一个全局变量。

回答by Douglas Franco

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