22

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.

4

8 Answers 8

30

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
Sign up to request clarification or add additional context in comments.

3 Comments

This means that gcc can't make functions inline across different source files?
@CrouchingKitten Not the compiler, but the linker might be able to do some link-time optimizations which might include cross-TU (Translation Unit) inlining.
Note that the library has to be after the object files when calling gcc. file1.o file2.o libfoo.a and not libfoo.a file1.o file2.o
12

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.

Comments

10

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.

1 Comment

Not src1.c src2.c src3.c source files for ld but src1.o src2.o src3.o object files!
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

2 Comments

Does not exist ? g++: error: unrecognized command line option ‘--combine’ gcc --version gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
It was removed in gcc 4.6.
2

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

1 Comment

Beware of name clashes. It will be painful if say file1.c and file2.c declares the same static variable.
2

Just want to say. This is a missing feature.

Why?

say cc can be clang or gcc

cc -c t1.c t2.c (multi-files at once)

vs

cc -c t1.c 
cc -c t2.c
...
(one by one)

What is the difference?

The output .o is same, though the one by one way can specify -o name which may useful when you use cc in your customized toolchain.

The difference is performance. Besides the time for starting up cc, if multi files use same set of .h header files(In most case they will). The header files can load once instead of per file.

Header files could be huge if you use several libraries. A typical .pch (pre-compiled-header) file can as large as 10MB.

In real life test on my small project. I combined all .cpp into one .cpp using #include "others.cpp" , the compile time reduced from 600 ms to 300 ms.

The c/cpp compiles slow, so this could be so useful to be a feature.

could like

cc -c t1.c -o t1.o  t2.c -o t2.o (specify output name per file)

or

cc -c t1.c  t2.c -o mylib.a (If some one say .o can't be combined
   , then link to a lib in one command line)

I hope clang or gcc develop can see this. But not bothering to wait for a fix even they could agreed this opinion.(argue among open source developers can be very hopeless, unless they realize something from self). You can try the method mentioned in other answers like

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

1 Comment

It actually makes sense to reduce disk-usage while compiling, and probably provide a wider in-memory idea for the optimizer to work with. Similarly, 'single header file packers'. Other advantages include the possibilities to build something small/specific without going through Makefiles
1

try using

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

it works for me.

2 Comments

This will generate a binary file, which clearly is not what the question ask for (object file). Please review your answer or delete it to not confuse other people.
This creates 3 object files, and links them into one executable file. Because the object file are separate, the compiler is not able to in-line or otherwise optimise cross-module calls and references.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.