Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c++ - 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 one source file for each object file. I want to have multiple files go into some of them. Is there any way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...