且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-04-10 01:46:34

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

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

如果你想合并编译的文件,通常使用 将它合并到一个静态库中ar 命令:

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

$ 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

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

Or linking with it as a library with the -l flag

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