简体   繁体   English

库不是仅限标题的原因是什么?

[英]What is the reason for a library to not be header-only?

I really like using header-only libraries, as they are really easy to use (no linker issues or having to compile the library separately). 我真的很喜欢使用仅限标头的库,因为它们非常容易使用(没有链接器问题或者必须单独编译库)。 For example most of the Boost library is header-only. 例如,大多数Boost库都是仅标题库。 But then again there are some parts, like boost::python, which requires to be build before. 但是又有一些部分,比如boost :: python,需要先构建。 Is this a design choice or a technical necessity? 这是设计选择还是技术必需品?

I gave Boost as an example but would appreciate a more general answer if possible. 我给了Boost作为一个例子,但如果可能的话,我会很感激。

The original reason to use compiled libraries is to spare compilation time. 使用编译库的最初原因是为了节省编译时间。 Libraries can be big. 图书馆可能很大。 They can be huge. 它们可能很大。

Another argument is that they keep the source code apart. 另一个论点是他们将源代码分开。 There is still a good deal of the universe which is not open-sourced. 还有很多宇宙不是开源的。

In favor of header only: 仅支持标题:

  • Less time to set up / import 设置/导入的时间更短
  • No linking trouble 没有连接麻烦

Against header only: 仅反对标题:

  • Collisions between headers unresolvable with compile-time firewall between them 标头之间的冲突无法解析,它们之间有编译时防火墙
  • No dependency hiding possible 没有依赖隐藏可能
  • Increases compile time for each target that includes its headers 增加包含其标头的每个目标的编译时间
  • Adds to symbol pile exported from target. 添加到从目标导出的符号堆。 Your simple test file may now export a few thousand symbols. 您的简单测试文件现在可以导出几千个符号。
  • (at extremes) May reduce separation between COMDAT-foldable symbols, causing multiple copies of your symbols to be irremovable from the target output files, causing bloat. (极端情况下)可能会减少COMDAT可折叠符号之间的分离,导致符号的多个副本无法从目标输出文件中删除,从而导致膨胀。 This should only happen at 32k+ symbols on ELF, but happens much earlier on other targets (such as Mach-O). 这应该只发生在ELF上的32k +符号上,但在其他目标(例如Mach-O)上发生得更早。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM