您如何减少 Visual C++ 项目(本机 C++)的编译时间和链接时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/364240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do YOU reduce compile time, and linking time for Visual C++ projects (native C++)?
提问by Brian R. Bondy
How do YOU reduce compile time, and linking time for VC++ projects (native C++)?
您如何减少 VC++ 项目(本机 C++)的编译时间和链接时间?
Please specify if each suggestion applies to debug, release, or both.
请指定每个建议是否适用于调试、发布或两者。
回答by Johannes Schaub - litb
It may sound obvious to you, but we try to use forward declarations as much as possible, even if it requires to write out long namespace names the type(s) is/are in:
这对您来说可能很明显,但我们尝试尽可能多地使用前向声明,即使它需要写出类型所在的长命名空间名称:
// Forward declaration stuff
namespace plotter { namespace logic { class Plotter; } }
// Real stuff
namespace plotter {
namespace samples {
class Window {
logic::Plotter * mPlotter;
// ...
};
}
}
It greatly reduces the time for compiling also on others compilers. Indeed it applies to all configurations :)
它也大大减少了在其他编译器上编译的时间。实际上它适用于所有配置:)
回答by 1800 INFORMATION
Use the Handle/Body pattern(also sometimes known as "pimpl", "adapter", "decorator", "bridge" or "wrapper"). By isolating the implementation of your classes into your .cpp files, they need only be compiled once. Most changes do not require changes to the header file so it means you can make fairly extensive changes while only requiring one file to be recompiled. This also encourages refactoring and writing of comments and unit tests since compile time is decreased. Additionally, you automatically separate the concerns of interface and implementation so the interface of your code is simplified.
使用Handle/Body 模式(有时也称为“pimpl”、“适配器”、“装饰器”、“桥接器”或“包装器”)。通过将类的实现隔离到 .cpp 文件中,它们只需要编译一次。大多数更改不需要更改头文件,因此这意味着您可以进行相当广泛的更改,而只需要重新编译一个文件。这也鼓励重构和编写注释和单元测试,因为编译时间减少了。此外,您会自动分离接口和实现的关注点,从而简化代码的接口。
回答by Daniel Earwicker
If you have large complex headers that must be included by most of the .cpp files in your build process, and which are not changed very often, you can precompile them. In a Visual C++ project with a typical configuration, this is simply a matter of including them in stdafx.h. This feature has its detractors, but libraries that make full use of templates tend to have a lot of stuff in headers, and precompiled headers are the simplest way to speed up builds in that case.
如果您的构建过程中的大多数 .cpp 文件必须包含大型复杂头文件,并且不会经常更改,则可以预编译它们。在具有典型配置的 Visual C++ 项目中,这只是将它们包含在 stdafx.h 中的问题。这个特性有它的反对者,但是充分利用模板的库往往在头文件中有很多东西,在这种情况下,预编译头文件是加速构建的最简单方法。
回答by Drew Dormann
These solutions apply to both debug and release, and are focused on a codebase that is already large and cumbersome.
这些解决方案适用于调试和发布,并且专注于已经庞大而繁琐的代码库。
Forward declarations are a common solution.
前向声明是一种常见的解决方案。
Distributed building, such as with Incredibuild is a win.
分布式构建,例如使用 Incredibuild 是一种胜利。
Pushing code from headers down into source files can work. Small classes, constants, enums and so on might start off in a header file simply because it could havebeen used in multiple compilation units, but in reality they are only used in one, and could be moved to the cpp file.
将头文件中的代码向下推送到源文件中是可行的。小类、常量、枚举等可能在头文件中开始,因为它可以在多个编译单元中使用,但实际上它们只在一个编译单元中使用,并且可以移动到 cpp 文件中。
A solution I haven't read about but have used is to split large headers. If you have a handful of very large headers, take a look at them. They may contain related information, and may also depend on a lot of other headers. Take the elements that have no dependencies on other files...simple structs, constants, enums and forward declarations and move them from the_world.h
to the_world_defs.h
. You may now find that a lot of your source files can now include only the_world_defs.h
and avoid including all that overhead.
我没有读过但使用过的一个解决方案是拆分大标题。如果您有一些非常大的标题,请查看它们。它们可能包含相关信息,也可能依赖于许多其他标头。获取不依赖于其他文件的元素......简单的结构、常量、枚举和前向声明,并将它们从the_world.h
到the_world_defs.h
. 您现在可能会发现您的许多源文件现在可以只包含the_world_defs.h
并避免包含所有这些开销。
Visual Studio also has a "Show Includes" option that can give you a sense of which source files include many headers and which header files are most frequently included.
Visual Studio 还具有“显示包含”选项,可以让您了解哪些源文件包含许多头文件以及哪些头文件最常包含。
For very common includes, consider putting them in a pre-compiled header.
对于非常常见的包含,请考虑将它们放在预编译的头文件中。
回答by OJ.
I use Unity Builds(Screencast located here).
我使用Unity Builds(此处的截屏视频)。
回答by crashmstr
We use Xoreax's Incredibuildto run compilation in parallel across multiple machines.
我们使用Xoreax 的 Incredibuild在多台机器上并行运行编译。
回答by David Norman
回答by Pawe? Hajdan
Also an interesting article from Ned Batchelder: http://nedbatchelder.com/blog/200401/speeding_c_links.html(about C++ on Windows).
还有一篇来自 Ned Batchelder 的有趣文章:http: //nedbatchelder.com/blog/200401/speeding_c_links.html(关于 Windows 上的 C++)。
回答by Pawe? Hajdan
Our development machines are all quad-core and we use Visual Studio 2008 supports parallel compiling. I am uncertain as to whether all editions of VS can do this.
我们的开发机器都是四核的,我们使用Visual Studio 2008支持并行编译。我不确定是否所有版本的 VS 都可以做到这一点。
We have a solution file with approximately 168 individual projects, and compile this way takes about 25 minutes on our quad-core machines, compared to about 90 minutes on the single core laptops we give to summer students. Not exactly comparable machines but you get the idea :)
我们有一个包含大约 168 个单独项目的解决方案文件,在我们的四核机器上以这种方式编译大约需要 25 分钟,而在我们提供给暑期学生的单核笔记本电脑上编译需要大约 90 分钟。不完全可比的机器,但你明白了:)
回答by Matt Shaw
With Visual C++, there is a method, some refer to as Unity, that improves link time significantly by reducing the number of object modules.
在 Visual C++ 中,有一种方法,有些人称之为 Unity,它通过减少对象模块的数量来显着缩短链接时间。
This involves concatenating the C++ code, usually in groups by library. This of course makes editing the code much more difficult, and you will run into namespace collisions unless you use them well. It keeps you from using "using namespace foo";
这涉及连接 C++ 代码,通常按库分组。这当然会使编辑代码变得更加困难,除非您很好地使用它们,否则您将遇到命名空间冲突。它使您无法使用“使用命名空间 foo”;
Several teams at our company have elaborate systems to take the normal C++ files and concatenate them at compile time as a build step. The reduction in link times can be enormous.
我们公司的几个团队都有精心设计的系统来获取普通的 C++ 文件,并在编译时将它们连接起来作为构建步骤。链接时间的减少可能是巨大的。