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.

Andry
  • 16,172
  • 27
  • 138
  • 246

8 Answers8

28

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
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.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
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.

P0W
  • 46,614
  • 9
  • 72
  • 119
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

david
  • 2,435
  • 1
  • 21
  • 33
  • Does not exist ? g++: error: unrecognized command line option ‘--combine’ gcc --version gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7) – gaoithe Mar 31 '17 at 13:41
  • It [was removed](https://stackoverflow.com/questions/26804314/gcc-error-unrecognized-command-line-option-combine) in gcc 4.6. – Hedede Jul 11 '17 at 06:39
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
rubenvb
  • 74,642
  • 33
  • 187
  • 332
1

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"
neoedmund
  • 561
  • 7
  • 15
  • 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 – DiegoJArg Feb 20 '23 at 17:53
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.

Dayal rai
  • 6,548
  • 22
  • 29
0

try using

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

it works for me.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    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. – Manoel Vilela Sep 12 '17 at 14:38
  • 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. – david Mar 15 '19 at 03:33