C语言 将多个 C 源文件编译成一个唯一的目标文件

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

Compile multiple C source fles into a unique object file

cgcclinker

提问by Andry

I have some C source files and I am using gcc. I basically want to compile all of them and create one single object file. When I try:

我有一些 C 源文件,我正在使用gcc. 我基本上想编译所有这些并创建一个单一的目标文件。当我尝试:

gcc -c src1.c src2.c src3.c -o final.o

I get:

我得到:

gcc: cannot specify -o with -c or -S with multiple files

If I try:

如果我尝试:

gcc -c src1.c src2.c src3.c

I get three different object files. How can I tell gccto compile all files to return one single object file (I also want to specify its name)? Thank you.

我得到三个不同的目标文件。我怎样才能告诉gcc编译所有文件以返回一个单一的目标文件(我也想指定它的名称)?谢谢你。

Maybe there is another more common approach to this, in this case please tell me.

也许还有另一种更常见的方法,在这种情况下,请告诉我。

回答by Some programmer dude

You can't compile multiple source files into a single object file. An object file is the compiled result of a single source file and its headers (also known as a translation unit).

您不能将多个源文件编译为单个目标文件。目标文件是单个源文件及其头文件(也称为翻译单元)的编译结果。

If you want to combine compiled files, it's usually combined into a static library using the arcommand:

如果要合并编译后的文件,通常使用以下ar命令合并成静态库:

$ ar cr libfoo.a file1.o file2.o file3.o

You can then use this static library when linking, either passing it directly as an object file:

然后你可以在链接时使用这个静态库,或者直接将它作为目标文件传递:

$ gcc file4.o libfoo.a -o myprogram

Or linking with it as a library with the -lflag

或者将其链接为带有-l标志的库

$ gcc file4.o -L. -lfoo -o myprogram

回答by Didier Trosset

This is not a usual way to proceed, but you can easily achieve what you want by creating a new final.cfile with the following content.

这不是一种通常的处理方式,但是您可以通过创建一个final.c包含以下内容的新文件来轻松实现您想要的。

#include "src1.c"
#include "src2.c"
#include "src3.c"

Then you can compile it as follows.

然后你可以编译它如下。

gcc -c final.c -o final.o

Note that there may be issues, read compilation errors, even if each file compiles successfully when compiled separately. This tend to happen especially with macro definitions and includes, when mergingyour source files into a single one this way.

注意可能会出现问题,读取编译错误,即使每个文件单独编译时都编译成功。当以这种方式将源文件合并为一个时,这种情况尤其容易发生在宏定义和包含中。

回答by P0W

May be you're looking for this :

可能你正在寻找这个:

ld -r src1.o src2.o src3.o -o final.o

But its always nice to have them archived, using

但是将它们存档总是很好,使用

ar rvs libmy.a file1.o file2.o file3.o

ar rvs libmy.a file1.o file2.o file3.o

Then use -lmyto link.

然后使用-lmy链接。

回答by david

To combine multiple source files into a single object file (at least since gcc 4.1), use the compiler/linker option --combine

要将多个源文件合并为一个目标文件(至少自 gcc 4.1 起),请使用编译器/链接器选项 --combine

(edit) later replaced with the compiler option -flto, with linking automatic depending on the compilation state: Requirements to use flto

(编辑)后来替换为编译器选项 -flto,根据编译状态自动链接:使用 flto 的要求

回答by rubenvb

You can always pipe the files to GCC:

您始终可以将文件通过管道传输到 GCC:

join file1.c file2.c file3.c | gcc -x c -c -o single.o -

Don't forget the option specifying the language, "-x c", which now cannot be deduced from the file extension. You tell GCC to accept input from stdin with the single trailing dash "-".

不要忘记指定语言的选项“-x c”,现在无法从文件扩展名中推断出该选项。您告诉 GCC 接受来自 stdin 的输入,并带有单个尾随破折号“-”。

Note that this is equivalent to compiling a file "final.c" with following contents:

请注意,这等效于使用以下内容编译文件“final.c”:

#include "file1.c"
#include "file2.c"
#include "file3.c"

With

gcc -c -o single.o final.c

回答by Dayal rai

Either you make a final.cwhich will include all .cfiles then you can compile this final using gcc -c final.ccommand.

要么你制作一个final.c包含所有.c文件的文件,然后你就可以编译这个最终的 usinggcc -c final.c命令。

or

或者

Another method is to use archive.Build all files to get respective .othen archive then in one library which will have all those .ofile.

另一种方法是使用 archive.Build 所有文​​件来获取各自的.o然后归档,然后在一个包含所有这些.o文件的库中。

回答by Himanshu Vashishth

try using

尝试使用

gcc -o final file1.c file2.c file3.c

it works for me.

这个对我有用。