Search code examples
c++cmakeboost-asio

Understanding boost.asio's BOOST_ASIO_SEPARATE_COMPILATION


I am trying to understand when to apply the BOOST_ASIO_SEPARATE_COMPILATION define when building boost.asio. In the linked documentation it states

By default, Boost.Asio is a header-only library. However, some developers may prefer to build Boost.Asio using separately compiled source code.

I am not sure what is meant by "separately compiled source code here". Would this correspond to a separately compiled static library, a shared library or maybe an externally supplied static / shared library?

To take my code as an example:

I create a few different static libraries in the same project (*.a files) which all include various boost.asio headers. They are then all linked to the executable in my root CMakeLists.txt

# initial cmake setup ... then:
add_executable(app main.cpp)

target_link_libraries(app PRIVATE my_lib_using_boost_1 my_lib_using_boost_2)

Is it necessary in this case to add the BOOST_ASIO_SEPARATE_COMPILATION like:

add_compile_definition(BOOST_ASIO_SEPARATE_COMPILATION)

or is this only necessarily when boost.asio is supplied in a separate, external archive like boost.a that I do not have compiled myself?


Solution

  • It is only necessary if you link the implementation bits explicitly:

    However, some developers may prefer to build Boost.Asio using separately compiled source code. To do this, add #include <boost/asio/impl/src.hpp> to one (and only one) source file in a program, then build the program with BOOST_ASIO_SEPARATE_COMPILATION defined in the project/compiler settings. Alternatively, BOOST_ASIO_DYN_LINK may be defined to build a separately-compiled Boost.Asio as part of a shared library.

    That's from the docs

    As to reasons why "some developers may prefer to build Boost.Asio using separately compiled source code" the only valid reason I can think of is slightly improved compilation times.