Boost Chrono Header Only versus Linked

363 views Asked by At

The Boost Chrono Documentation says that it is possible to use the chrono library as a header only library. I don't see anything that mentions the limitations to using it as a header only library. Besides the obvious difference of not needing to link against boost_chrono, what changes when I define the BOOST_CHRONO_HEADER_ONLY macro?

I am interesting in using the library for the the chrono_io features. I would prefer to use the header only version but want to know what I lose by doing so. If there isn't any difference why does the linked version exist?

#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";

}

Compiled with

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

Running both produces the same output with the current time.

1

There are 1 answers

0
sehe On BEST ANSWER

If there's no documented difference, you can be confident that there is none.

The technical difference of course, is that the binary will be larger (containing all the required definitions what would otherwise be in the shared library).

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.