C语言 使用gcc编译多个C文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18777326/
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:26:06  来源:igfitidea点击:

Compiling multiple C files with gcc

cgcc

提问by vaindil

I have two files, main.oand modules.o, and I'm trying to compile them so that main.ocan call functions in modules.o. I was explicitly told not to try #include module.o. I really don't know what I should be doing instead. I tried a few different versions of gcc(such as gcc -x c driver main.o modules.o), but nothing I get works: the compiler continuously returns

我有两个文件main.omodules.o, 我正在尝试编译它们以便main.o可以调用modules.o. 我被明确告知不要尝试#include module.o。我真的不知道我应该做什么。我尝试了几个不同版本的gcc(例如gcc -x c driver main.o modules.o),但没有任何效果:编译器不断返回

error: called object is not a function

The .ofiles are my source code files (I was instructed to put my source code in files with extension .o.) What do I do to compile this?

这些.o文件是我的源代码文件(我被指示将我的源代码放在扩展名为.o.的文件中。)我该怎么编译它?

采纳答案by Manoj Pandey

You should define the functions that you want to call from modules.cinto main.cinto a header file, let us say modules.h, and include that header file in main.c. Once you have the header file, please compile both of the files together: gcc main.c modules.c -o output

你应该定义的功能,你想从呼叫modules.c进入main.c到一个头文件中,我们可以说modules.h,和包括在头文件main.c。有了头文件后,请一起编译这两个文件:gcc main.c modules.c -o output



Two additional notes. First, modules.ois an object file and it should not be included in a C source file. Second, we cannot have a C file have a .oextension. You should actually get an error when compiling a .ofile. Something like:

两个补充说明。首先,modules.o是一个目标文件,它不应包含在 C 源文件中。其次,我们不能让 C 文件有.o扩展名。编译.o文件时,您实际上应该会收到错误消息。就像是:

$ cat t.o
int main() {
    int x = 1;
    return 0;
}
$
$ gcc t.o
ld: warning: in t.o, file is not of required architecture
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$

回答by Crowman

If you have your two source files, you can compile them into object files without linking, as so:

如果您有两个源文件,则可以将它们编译为目标文件而无需链接,如下所示:

gcc main.c -o main.o -c
gcc module.c -o module.o -c

where the -cflag tells the compiler to stop after the compilation phase, without linking. Then, you can link your two object files as so:

-c标志告诉编译器在编译阶段后停止,而不进行链接。然后,您可以像这样链接两个目标文件:

gcc -o myprog main.o module.o

This is all perfectly normal behavior, you'll usually get your makefile to compile things separately and link them at the end, so you don't have to recompile every single source file every time you change one of them.

这是完全正常的行为,您通常会让您的 makefile 单独编译并在最后链接它们,因此您不必每次更改其中一个源文件时都重新编译每个源文件。

Talking about main.o"calling functions in" module.ois perfectly fine, but an .ofile is not a sourcefile, it's a compiled objectfile. If "put my source code in files with extension .o" actually meant "compile my source code into files with extension .o" then the situation would make a whole lot more sense.

谈论main.o“调用函数”module.o是完全没问题的,但.o文件不是文件,而是编译后的目标文件。如果“将我的源代码放在带有扩展名的文件中.o”实际上意味着“将我的源代码编译成带有扩展名的文件.o”,那么这种情况会更有意义。

回答by Sarp Kaya

You should be including .hfiles which are "headers". So if your main file is using modules then you should include module's header file.

您应该包含.h作为“标题”的文件。因此,如果您的主文件使用模块,那么您应该包含模块的头文件。

回答by user2425240

program: main.o 
    gcc -o main main.c anotherSource.c

This works for me.

这对我有用。