How to use multiple source files to create a single object file with gcc(如何使用多个源文件通过 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
创建一个单独的统一文件"
Create a separate "unity file"
// This is the entire file (unity.cpp)
#include "file1.cpp"
#include "file2.cpp"
// more if you want...
然后
g++ -c unity.cpp
在很多情况下,这还具有编译和链接速度更快的优点(因为 file1.cpp 和 file2.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.
这篇关于如何使用多个源文件通过 gcc 创建单个目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用多个源文件通过 gcc 创建单个目标文件
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
