Compile multiple C source fles into a unique object file

余生长醉 提交于 2019-12-17 23:29:47

问题


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

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


回答1:


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

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

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



回答2:


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

#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 merging your source files into a single one this way.




回答3:


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

Then use -lmy to link.




回答4:


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

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




回答5:


Either you make a final.c which will include all .c files then you can compile this final using gcc -c final.c command.

or

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




回答6:


try using

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

it works for me.




回答7:


You can always pipe the files to 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 "-".

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

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

With

gcc -c -o single.o final.c


来源:https://stackoverflow.com/questions/18182176/compile-multiple-c-source-fles-into-a-unique-object-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!