有什么优点和优点?特别是在 GNU/Linux 环境/工具链中预编译头的缺点?

发布于 2024-08-03 08:22:07 字数 107 浏览 6 评论 0原文

预编译头似乎可以在大型项目中节省大量时间,但也似乎是一个令人头疼的问题,有一些陷阱。

有什么优点和优点?使用预编译头的缺点,特别是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

秋意浓 2024-08-10 08:22:07

预编译头的唯一潜在好处是,如果您的构建速度太慢,预编译头可能会加快速度。潜在的缺点:

  • 需要更多的 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.

洋洋洒洒 2024-08-10 08:22:07

我无法与 GNU/gcc/linux 对话,但我已经处理过 vs2005 中的预编译头:

优点:

  • 当您有大量模块的大头文件时,可以节省编译时间
    包括。
  • 适用于变化很大的标头(例如来自第三方的标头)
    很少。

缺点:

  • 如果您将它们用于变化很大的标题,
    它可以增加编译时间。
  • 设置和维护可能很繁琐。
  • 在某些情况下,标题的更改显然会被忽略
    如果不强制编译预编译头。

I can't talk to GNU/gcc/linux, but I've dealt with pre-compiled headers in vs2005:

Pros:

  • Saves compile time when you have large headers that lots of modules
    include.
  • Works well on headers (say from a third party) that change very
    infrequently.

Cons:

  • If you use them for headers that change a lot,
    it can increase compile time.
  • Can be fiddly to set up and maintain.
  • There are cases where changes to headers are apparently ignored
    if you don't force the pre-compiled header to compile.
痴情 2024-08-10 08:22:07

gcc、g++、gfortran 的 ccache 缓存前端对我来说非常有用。正如其网站所说

ccache 是编译器缓存。它充当
C/C++ 的缓存预处理器
编译器,使用 -E 编译器
开关和哈希来检测何时
编译可以满足
缓存。这通常会导致 5 到 10
普通编译的加速倍数。

在 Debian / Ubuntu 上,只需执行“apt-get install ccache”并在 /usr/local/bin 中创建名为 gcc< 的软链接/code>、g++gfortranc++、... 指向 /usr/bin/ccache

[编辑] 为了使这一点更加明确,以响应一些早期的评论:这通过缓存更大的编译步骤块来提供本质上预编译的标头和源。所以它使用了类似于预编译头的想法,并进一步发展。加速速度可能非常惊人——正如网站所说,速度提升了 5 到 10 倍。

The ccache caching frontend to gcc, g++, gfortran, ... works great for me. As its website says

ccache is a compiler cache. It acts as
a caching pre-processor to C/C++
compilers, using the -E compiler
switch and a hash to detect when a
compilation can be satisfied from
cache. This often results in a 5 to 10
times speedup in common compilations.

On Debian / Ubuntu, just do 'apt-get install ccache' and create soft-links in, say, /usr/local/bin with names gcc, 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.

只是一片海 2024-08-10 08:22:07

对于普通 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.

浅唱々樱花落 2024-08-10 08:22:07

GNU gcc 文档讨论了预编译头文件可能存在的缺陷。

The GNU gcc documentation discusses possible pitfalls with pre-compiled headers.

我在 Qt 项目中使用 PCH,该项目使用 cmake 作为构建系统,它节省了大量时间。我获取了一些 PCH cmake 脚本,这些脚本需要一些调整,因为它们相当旧,但通常比我预期的更容易设置。我必须补充一点,我不是 cmake 专家。

我现在一次性包含 Qt 的很大一部分(QtCore、QtGui、QtOpenGL)和一些稳定的标头。

优点:

  • 对于 Qt 类,不需要前向声明,当然也不需要包含。
  • 快速地。
  • 易于设置。

缺点:

  • 您不能将 PCH 包含在标头中。这不是什么大问题,除非您使用 Qt 并让构建系统单独翻译 moc 文件,这恰好是我的配置。在这种情况下,您需要在标头中 #include qt 标头,因为 moc 是根据标头进行分类的。解决方案是在标头中的 #include 周围放置额外的包含防护。

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:

  • For Qt classes,no forward declarations are needed, and of course no includes.
  • Fast.
  • Easy to setup.

Cons:

  • You can't include the PCH include in headers. This isn't much of a problem, exept you use Qt and let the build system translate the moc files seperatly, which happens to be exactly my configuration. In this case, you need to #include the qt headers in your headers, because the mocs are genreted from headers. Solution was to put additional include guards around the #include in the header.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文