公共库发布技巧之header only

原创
2021/10/28 21:17
阅读数 313

In the context of the C or C++ programming languages, a library is called header-only if the full definitions of all macros, functions and classes comprising the library are visible to the compiler in a header file form.[1] Header-only libraries do not need to be separately compiled, packaged and installed in order to be used. All that is required is to point the compiler at the location of the headers, and then #include the header files into the application source. Another advantage is that the compiler's optimizer can do a much better job when all the library's source code is available.

The disadvantages include:

  • brittleness – most changes to the library will require recompilation of all compilation units using that library
  • longer compilation times – the compilation unit must see the implementation of all components in the included files, rather than just their interfaces
  • machine-code bloat (arguably) – the necessary use of inline statements in non-class functions can lead to code bloat by over-inlining.

Nonetheless, the header-only form is popular because it avoids the (often much more serious) problem of packaging.

For C++ templates, including the definitions in header is the only way to compile, since the compiler needs to know the full definition of the templates in order to instantiate.

 

简而言之是一种库发布只对外提供.h 或者.hpp文件即可

asio、rapidjson、rapidxml等均采用这种模式发布

 

header only做公共库可以设计为兼容模式

既传统的。h+dll/lib和header only混合发布

只需要一个简单的宏添加在头文件尾部就可以搞定 header_only_XXXLibrary

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部