问题描述
这个问题可能有点奇怪,但我怎样才能加快 g++ 编译时间?我的 C++ 代码大量使用 boost 和模板.我已经尽可能多地移出头文件并使用 -j 选项,但编译(和链接)仍然需要很长时间.
This question is perhaps somehow odd, but how can I speed up g++ compile time? My C++ code heavily uses boost and templates. I already moved as much as possible out of the headers files and use the -j option, but still it takes quite a while to compile (and link).
是否有任何工具可以分析我的代码并指出编译器的瓶颈?或者可以以某种方式分析在我的代码上运行的编译器?这真的很好,因为有时我会有这样的印象,我花了太多时间盯着编译器控制台日志......
Are there any tools out there which analyse my code and point out bottle-necks for the compiler? Or can one somehow profile the compiler running on my code? This would be really nice, because sometimes I have the impression, that I spent too much time staring at the compiler console log ...
推荐答案
对我最有用的:
- 在 RAM 文件系统上构建.这在 Linux 上是微不足道的.您可能还希望在 RAM 文件系统上保留一份通用头文件(预编译的或实际的 .h 文件)的副本.
- 预编译头.我每个(主要)库(例如 Boost、Qt、stdlib)都有一个.
- 在可能的情况下声明而不是包含类.这减少了依赖性,从而减少了更改头文件时需要重新编译的文件数量.
- 并行化 make.这通常会根据具体情况有所帮助,但我在全局范围内使用
-j3
进行 make.不过,请确保您的 Makefile 中的依赖关系图正确无误,否则您可能会遇到问题. - 如果您不测试执行速度或代码大小(并且您的计算机足够快,您不必太在意(可能很小)性能损失),请使用
-O0
. - 每次保存时都要编译.有些人不喜欢这样,但它可以让您及早发现错误并可以在后台完成,从而减少您在编写完成并准备好测试时必须等待的时间.
- Build on a RAM filesystem. This is trivial on Linux. You may want to keep a copy of common header files (precompiled or the actual .h files) on the RAM filesystem as well.
- Precompiled headers. I have one per (major) library (e.g. Boost, Qt, stdlib).
- Declare instead of include classes where possible. This reduces dependencies, thus reduces the number of files which need to be recompiled when you change a header file.
- Parallelize make. This usually helps on a case-by-case basis, but I have
-j3
globally for make. Make sure your dependency graphs are correct in your Makefile, though, or you may have problems. - Use
-O0
if you're not testing execution speed or code size (and your computer is fast enough for you not to care much about the (probably small) performance hit). - Compile each time you save. Some people don't like this, but it allows you to see errors early and can be done in the background, reducing the time you have to wait when you're done writing and ready to test.
这篇关于如何加快 g++ 编译时间(使用大量模板时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!