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

And*_*rew 18 c

我开始使用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;
}
Run Code Online (Sandbox Code Playgroud)

如何将C函数移动到单独的文件中?仅供参考:我正在使用gcc.

更新:这些答案非常有用,谢谢.现在似乎我的简化示例不够好,因为我意识到我的程序编译失败的原因是因为我在我的函数中使用了一个全局变量.

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

int counter = 0;

void func1(void) {
  printf("Function 1!\n");
  counter++;
}

int main(void) {
  func1();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

将这些函数移动到外部文件不起作用,因为它们需要引用此全局变量:

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

int counter = 0;

int main(void) {
  func1();
  counter = 100;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Raj*_*nde 28

好的.开始了.

你的main.c文件

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

int main(void) {
  func1();
  func2();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

你的functions.h文件

void func1(void);
void func2(void);
Run Code Online (Sandbox Code Playgroud)

你的functions.c文件

#include "functions.h"

void func1(void) {
  printf("Function 1!\n");
}

void func2(void) {
  printf("Function 2!\n");
}
Run Code Online (Sandbox Code Playgroud)

编译它:

gcc -o main.exe main.c functions.c
Run Code Online (Sandbox Code Playgroud)

  • 您将创建头文件,因为通常您有多个使用这些函数的源文件(因此除了 `main.c` 之外,还有 `aux.c` 和 ...),并且它们不能全部使用`#include "func1.c"` 技巧;只有一个源文件可以做到这一点,但其他源文件仍然需要函数声明,因此您需要一个头文件。 (2认同)

Dav*_*dek 9

最常见的方法是将函数原型放在头文件中,将函数实现放在源文件中.例如:

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
Run Code Online (Sandbox Code Playgroud)

func1.c

#include "func1.h"

// defines a variable
int var1 = 512;

// defines a function
void func1(void) {
    printf("Function 1!\n");
}
Run Code Online (Sandbox Code Playgroud)

func2.h:

#ifndef MY_FUNC2_H
#define MY_FUNC2_H
#include <stdio.h>
void func2(void);
#endif
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

main.c中:

#include <stdio.h>
#include "func1.h"
#include "func2.h"

int main(void) {
    var1 += 512;
    func1();
    func2();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后,您将使用以下代码进行编译:

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
Run Code Online (Sandbox Code Playgroud)

我只在每个源/标题对中放置了一个函数用于说明.您可以只创建一个包含所有源文件原型的标头,也可以为每个源文件创建多个标头文件.关键是任何调用该函数的源文件都需要包含一个包含函数原型的头文件.

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


Som*_*ude 5

首先,您必须了解声明定义之间的区别。声明告诉编译器某些东西,如函数,存在。对于函数,定义是实际的函数实现。

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