有什么优点和优点?特别是在 GNU/Linux 环境/工具链中预编译头的缺点?
预编译头似乎可以在大型项目中节省大量时间,但也似乎是一个令人头疼的问题,有一些陷阱。
有什么优点和优点?使用预编译头的缺点,特别是在 Gnu/gcc/Linux 环境中使用它们的缺点?
Pre-compiled headers seem like they can save a lot of time in large projects, but also seem to be a pain-in-the-ass that have some gotchas.
What are the pros & cons of using pre-compiled headers, and specifically as it pertains to using them in a Gnu/gcc/Linux environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
预编译头的唯一潜在好处是,如果您的构建速度太慢,预编译头可能会加快速度。潜在的缺点:
需要更多的 Makefile 依赖才能正确使用;如果他们错了,你很快就会做出错误的东西。不好。
原则上,并非所有标头都可以预编译。 (考虑在#include 之前放置一些#define。)那么gcc 在哪些情况下实际上是正确的呢?您对这个前沿功能有多信任。
如果您的构建足够快,则没有理由使用预编译标头。如果您的构建速度太慢,我会考虑
购买更快的硬件,这比工资便宜
使用像 AT&T 这样的工具 nmake 或类似 ccache(德克是对的),两者都使用值得信赖的技术来避免重新编译。
The only potential benefit to precompiled headers is that if your builds are too slow, precompiled headers might speed them up. Potential cons:
More Makefile dependencies to get right; if they are wrong, you build the wrong thing fast. Not good.
In principle, not every header can be precompiled. (Think about putting some #define's before a #include.) So which cases does gcc actually get right? How much do you want to trust this bleeding edge feature.
If your builds are fast enough, there is no reason to use precompiled headers. If your builds are too slow, I'd consider
Buying faster hardware, which is cheap compared to salaries
Using a tool like AT&T nmake or like ccache (Dirk is right on), both of which use trustworthy techniques to avoid recompilations.
我无法与 GNU/gcc/linux 对话,但我已经处理过 vs2005 中的预编译头:
优点:
包括。
很少。
缺点:
它可以增加编译时间。
如果不强制编译预编译头。
I can't talk to GNU/gcc/linux, but I've dealt with pre-compiled headers in vs2005:
Pros:
include.
infrequently.
Cons:
it can increase compile time.
if you don't force the pre-compiled header to compile.
gcc、g++、gfortran 的 ccache 缓存前端对我来说非常有用。正如其网站所说
在 Debian / Ubuntu 上,只需执行“
apt-get install ccache
”并在/usr/local/bin
中创建名为gcc< 的软链接/code>、
g++
、gfortran
、c++
、... 指向/usr/bin/ccache
。[编辑] 为了使这一点更加明确,以响应一些早期的评论:这通过缓存更大的编译步骤块来提供本质上预编译的标头和源。所以它使用了类似于预编译头的想法,并进一步发展。加速速度可能非常惊人——正如网站所说,速度提升了 5 到 10 倍。
The ccache caching frontend to gcc, g++, gfortran, ... works great for me. As its website says
On Debian / Ubuntu, just do '
apt-get install ccache
' and create soft-links in, say,/usr/local/bin
with namesgcc
,g++
,gfortran
,c++
, ... that point to/usr/bin/ccache
.[EDIT] To make this more explicit in response to some early comments: This provides essentially pre-compiled headers and sources by caching a larger chunk of the compilation step. So it uses an idea that is similar to pre-compiled headers, and carries it further. The speedups can be dramatic -- a factor of 5 to 10 as the website says.
对于普通 C,我会避免使用预编译头。正如您所说,它们可能会导致问题,并且与常规编译相比,预处理时间非常短。
对于 C++,预编译头文件可能会节省大量时间,因为 C++ 头文件通常包含大型模板代码,其编译成本很高。我没有使用它们的实际经验,因此我建议您衡量一下在项目中节省了多少编译时间。为此,使用预编译头编译整个项目一次,然后删除单个目标文件,并测量重新编译该文件需要多长时间。
For plain C, I would avoid precompiled headers. As you say, they can potentially cause problems, and preprocessing time is really small compared to the regular compilation.
For C++, precompiled headers can potentially save a lot of time, as C++ headers often contain large template code whose compilation is expensive. I have no practical experience with them, so I recommend you measure how much savings in compilation you get in your project. To so so, compile the entire project with precompiled headers once, then delete a single object file, and measure how long it takes to recompile that file.
GNU gcc 文档讨论了预编译头文件可能存在的缺陷。
The GNU gcc documentation discusses possible pitfalls with pre-compiled headers.
我在 Qt 项目中使用 PCH,该项目使用 cmake 作为构建系统,它节省了大量时间。我获取了一些 PCH cmake 脚本,这些脚本需要一些调整,因为它们相当旧,但通常比我预期的更容易设置。我必须补充一点,我不是 cmake 专家。
我现在一次性包含 Qt 的很大一部分(QtCore、QtGui、QtOpenGL)和一些稳定的标头。
优点:
缺点:
I am using PCH in a Qt project, which uses cmake as build system, and it saves a lot of time. I grabbed some PCH cmake scripts, which needed some tweaking, since they were quite old but it generally was easier to set up than I expected. I have to add, I am not much of a cmake expert.
I am including now a big part of Qt (QtCore, QtGui, QtOpenGL) and a few stable headers at once.
Pros:
Cons: