开发者

How to use multiple source files to create a single object file with gcc

I'm using the -c option with g++ to create a bunch of object files, and it's only letting me specify o开发者_C百科ne source file for each object file. I want to have multiple files go into some of them. Is there any way to do this?


Others have mentioned archive, but another option is Unity builds.

Instead of:

g++ -c file1.cpp file2.cpp

Create a separate "unity file"

// This is the entire file (unity.cpp)
#include "file1.cpp"
#include "file2.cpp"
// more if you want...

Then

g++ -c unity.cpp

This also has the advantage of faster compilation and linking in many cases (because headers used by both file1.cpp and file2.cpp are only parsed once). However, if you put too many files in a single unity however then you'll find that you need to rebuild more sources than you wanted to, so you need to try and strike a balance.


You can use ld -r to combine the objects while keeping relocation information and leaving constructors unresolved:

ld -r -o everything.o object1.o object2.o ...


You can create archive which is a set of object files.

ar mylib.a file1.o file2.o

So effectively you have combined file1.cpp and file2.cpp into mylib.a.


You can utilize the ar command to create an archive for use by your program.


Using the solution from Peter Alexander is the main one that comes to mind.

But, keep in mind that by using this method, you'll have to compile your whole sources files each time. When your project grows bigger, compilation time can become a pain.

Furthermore, compiling several files on their own enable the use of the various cores on modern CPUs: each source file will be compiled in its own process, at full speed. Do not under-use the power of the multi cores.


I know you are asking about how to combine .cpp files into one object file. I assume your goal for doing this is to link the combined object at a later time with other individual object files. If this is the case you may be able to use a static or dynamic library instead. For your goals I suggest the dynamic library since you can do a single compile and link step skipping the generation of object files altogether. Using a command such as g++ -shared -fpic file1.cpp file2.cpp file3.cpp -o libtest.so you can combine your .cpp files that need combining into libraries. To compile the library(s) with your individual object files use a command such as g++ -ltest individual1.o individual2.o individual3.o -o myexecutable. In that final linking step I assume libtest.so is in your current directory. If its not in your current directory add a -L flag and the directory libtest.so is in.


In a sense, you can't. The compiler only compiles one file at a time. When you give it a list of files, either with -c or to turn into a single executable or shared library, it actually calls the compiler a bunch of times with some temporary output files (usually in /tmp or similar), and then links them after the fact.

The solution, then, is to only ask it to compile one file. Already mentioned is writing a file to compile

tocompile.cpp
--------------------
#include "file1.cpp"
#include "file2.cpp"

You could even autogenerate this file in your Makefile, but it turns out there is no need to do so, because the compiler is quite happy to read from - (standard input).

This means you can simply run

printf '#include "%s"\n' ./*.cpp | $CXX $CXXFLAGS -o combined.o -x c++ -

Note that the -x tells the compiler what language the input is, normally it guesses based on the compiler called (gcc vs g++ or similar), and the filename, but since the filename is -, we have to specify.

When doing this, assuming you are including your whole project, -fwhole-program in gcc is a good way to decrease the generated output size, note that for that to work, you need to also list all libraries needed.


The only solution I know of is to create a separate C++ file, which includes all of the files you want to compile, and compile that. A pretty bad solution, in my mind; generally, you want to increase the granularity of the object files, not reduce it.

The real question, I suppose, is what are you trying to achieve. Why do you want only a single object file?


We use -o option to create an object file...... By Default when we compile file using gcc or g++ we get object file named as a.out but we can change its name.... use following to compile file

gcc Filename.c -o NewFilename.out

then to run file you can use ./NewFilename.out

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜