编译多个C源FLES成一个独特的目标文件 [英] Compile multiple C source fles into a unique object file
问题描述
我有一些C源文件和我使用 GCC
。我基本上要编译所有的人创造一个单一的目标文件。当我尝试:
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:
gcc -c src1.c src2.c src3.c -o final.o
我得到:
gcc: cannot specify -o with -c or -S with multiple files
如果我尝试:
gcc -c src1.c src2.c src3.c
我得到三个不同的目标文件。我怎么能告诉 GCC
编译所有的文件返回一个单一的目标文件(我也想指定它的名字)?谢谢你。
I get three different object files. How can I tell gcc
to compile all files to return one single object file (I also want to specify its name)? Thank you.
也许有这另一种更常见的做法,在这种情况下,请告诉我。
Maybe there is another more common approach to this, in this case please tell me.
推荐答案
您不能编译多个源文件成一个单一的目标文件。一个目标文件就是一个源文件和头文件的编译的结果。
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.
如果你想编译的文件相结合,它通常使用 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 </ code>标记库
Or linking with it as a library with the -l
flag
$ gcc file4.o -L. -lfoo -o myprogram
这篇关于编译多个C源FLES成一个独特的目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!