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"