gpt4 book ai didi

c++ - Boost Chrono Header Only 与 Linked

转载 作者:行者123 更新时间:2023-11-30 04:56:58 30 4
gpt4 key购买 nike

Boost Chrono Documentation说可以将 chrono 库用作仅 header 库。我没有看到任何提到将其用作仅 header 库的限制的内容。除了不需要链接到 boost_chrono 的明显区别外,当我定义 BOOST_CHRONO_HEADER_ONLY 宏时有什么变化?

我对将库用于 chrono_io 功能很感兴趣。我宁愿使用仅 header 版本,但想知道这样做会损失什么。如果没有任何区别,为什么存在链接版本?

#define BOOST_CHRONO_VERSION 2

#include <boost/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>

int main(int argc, char **argv)
{
const auto now = boost::chrono::system_clock::now();

std::cout << "It is now "
<< boost::chrono::time_fmt(boost::chrono::timezone::local)
<< now << "\n";

}

编译为

g++ -std=c++11 -o chronoLinked main.cpp -lboost_system -lboost_chrono
g++ -std=c++11 -o chrono main.cpp -lboost_system -D BOOST_CHRONO_HEADER_ONLY

同时运行两者会产生与当前时间相同的输出。

最佳答案

如果没有记录的差异,您可以确信没有差异。

当然,技术上的区别在于二进制文件会更大(包含所有必需的定义,否则将包含在共享库中)。

For other libraries (like Boost System) electing the header-only variation will impact programs that e.g. link to other libraries that also used Boost System. The difference would be in globals being duplicated when they would be shared in the dynamic-linking scenario. A clear example of this would be the error categories.

If you want to be vigilant, look for (static) global data that would need to be shared, not duplicated. I'm not immediately aware of such data in Boost Chrono.

关于c++ - Boost Chrono Header Only 与 Linked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52248551/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com