如何使用多个源文件用gcc创建单个目标文件 [英] How to use multiple source files to create a single object file with gcc
问题描述
我使用与g ++的 -c 选项来创建一堆目标文件,它只允许我为每个目标文件指定一个源文件。我想有多个文件进入其中一些。有没有办法做这个?
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?
推荐答案
其他人提到归档,但另一个选项是 Unity builds 。
Others have mentioned archive, but another option is Unity builds.
而不是:
g++ -c file1.cpp file2.cpp
创建单独的unity文件
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
在许多情况下,这也有更快的编译和链接的优点(因为 file1.cpp
和 file2.cpp
只解析一次)。但是,如果你把太多的文件放在一个单一的unity,然而然后你会发现,你需要重建更多的来源,而不是你想要的,所以你需要尝试和一个平衡。
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.
这篇关于如何使用多个源文件用gcc创建单个目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!