使用 boost 库时加快编译/链接时间

发布于 2024-10-18 01:26:44 字数 259 浏览 3 评论 0原文

我正在使用 Boost 程序选项,并且需要用它编译非常小的 C++ 代码需要相当长的时间(10 秒甚至更多)。在没有 boost 库的情况下编译代码花了 1 秒。

知道如何使用 boost 库来提高编译/链接时间吗?它是跨平台的,所以我需要在 Mac OS X/Linux/PC 上编译代码。

I'm using Boost Program Options, and it takes quite a while (10 seconds or even more) for compiling very small C++ code with it. It took 1 second compiling the code without boost library.

Any idea how to boost compilation/link time with boost library? It's cross platform, so I need to compile the code with Mac OS X/Linux/PC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

执笏见 2024-10-25 01:26:44

除了通常的技巧之外,您实际上无能为力:

  • 最小化依赖项:仅引入您真正需要的 Boost 标头,并尽可能使用特定的标头(许多库都有一个“主”标头,例如 boost/thread.hpp,但也是具有特定标头的子目录,例如 boost/thread/shared_mutex.hpp),
  • 在可能的情况下,依赖前向声明而不是包含整个标头,
  • 如果可能的话,仅将标头包含在 .cpp 文件中。如果将其包含在标头中,则每次编译包含该标头的翻译单元时都必须编译它。作为一般经验法则,请尽量减少标头中的代码量,
  • 所有主要编译器都支持预编译标头。使用它们来减少编译时间,
  • 尝试 unity 构建。对于您的情况来说,这可能是也可能不是优势。

最后但并非最不重要的一点是,最后一个选择是不使用那些特定的 Boost 库。

有时,出于方便,我会尽早使用某些 Boost 库,如果/当编译时间变得太糟糕时,我会开始查看哪些库的编译成本很高,哪些库可以用相对简单的代码替换。通常,Boost 会因过于笼统的要求而受到阻碍。如果您不需要在 8 年前的编译器上工作的东西,或者不需要跨这么多不同类型工作的东西,那么您也许可以编写一个适合您的简单替代品,并且几乎不需要时间来编译。

There isn't really much you can do beyond the usual tricks:

  • minimize dependencies: pull in only the Boost headers you really need, and use as specific headers as possible (many libraries have a single "master" header, such as boost/thread.hpp, but also a subdirectory with specific headers, like boost/thread/shared_mutex.hpp),
  • where possible, rely on forward declarations instead of including the entire header,
  • if possible, include the header only in a .cpp file. If you include it in a header, it has to be compiled every time a translation unit which includes that header is compiled. As a general rule of thumb, try to minimize the amount of code you have in headers,
  • all major compilers support precompiled headers. Use those to cut down compilation time,
  • experiment with unity builds. That may or may not be an advantage in your case.

And last, but not least, a final option is just to not use those specific Boost libraries.

I sometimes use certain Boost libs early on, out of convenience, and if/when compilation time gets too bad, I start looking at which libraries are expensive to compile, and which ones could be replaced by relatively simple code. Often, Boost is encumbered by the requirement to be so general. If you don't need something that works on 8 year old compilers, or which doesn't have to work across quite so many different types, then you may be able to write a simple replacement which works for you, and takes almost no time to compile.

谎言 2024-10-25 01:26:44

我使用编译器防火墙习惯用法来减少编译时间,取得了很好的成功。

I had good success with the compiler firewall idiom to reduce compile times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文