Boost.Asio as header-only

16,047

Solution 1

AFAIK you can get the non-boost version of asio from http://think-async.com/Asio/AsioAndBoostAsio

"— Boost.Asio uses the Boost.System library to provide support for error codes ( boost::system::error_code and boost::system::system_error). Asio includes these under its own namespace ( asio::error_code and asio::system_error). The Boost.System version of these classes currently supports better extensibility for user-defined error codes.

— Asio is header-file-only and for most uses does not require linking against any Boost library. Boost.Asio always requires that you link against the Boost.System library, and also against Boost.Thread if you want to launch threads using boost::thread."

Solution 2

UPDATE – 07/25/2019:

As noted in the comment below by @OleThomsenBuus (thank you!), from Boost 1.69 onward, Boost.System is now header-only, so there's no need to jump through all these hoops to eliminate the need to link with it.

ORIGINAL ANSWER:

The accepted answer is 100% effective and recommended, but another option—if you really want/need to use Boost Asio—is to try compiling your application with -DBOOST_ERROR_CODE_HEADER_ONLY. Use of this macro (documented here) should get around the need to link with Boost.System. However, it's worth reading the caveats pointed out in this answer. In particular, you may need to create a 'dummy' CPP file containing:

#define BOOST_ERROR_CODE_HEADER_ONLY
#include <boost/system/error_code.hpp>

and disable optimization for that file only. (Personally, I didn't need to do this, but YMMV...)

Solution 3

I think bcp pulls the regex library because it can be used (and on Windows machines it is used by default). I expect that you can delete the regex library source files no problem. Make sure you add the correct compiler flags if you are compiler on windows (-DBOOST_DATE_TIME_NO_LIB and -DBOOST_REGEX_NO_LIB)

The details are from this page (which by the sounds of it you have already found).

I'm not sure how smart bcp is - I'm don't think you can pass it the defines given above that prevent it following the mscv route.

Share:
16,047

Related videos on Youtube

zaharpopov
Author by

zaharpopov

Коммунизм!

Updated on July 26, 2020

Comments

  • zaharpopov
    zaharpopov over 3 years

    I want to use ASIO library from Boost in my project. Its doc say it can be header-only if regex is not used and SSL not used. However, running bcp for asio pulls a very many libraies some of which are with sources so need compiling, bjam etc.

    Can I somehow use ASIO in project as only headers, without libs/source? I only need ASIO, not other part of Boost.

    EDIT: ASIO want Boost.System which has a lib to link - can this dependency not be so that I can use header only ASIO?

    • Sam Miller
      Sam Miller about 13 years
      Boost.Asio does require linking to Boost.System. Non-boost Asio does not, see Ralf's answer.
  • zaharpopov
    zaharpopov about 13 years
    I heard about non-boost ASIO but some places written it requies still Boost to build
  • zaharpopov
    zaharpopov about 13 years
    See my edit - ASIO also require Boost.System which is not only headers
  • Hasturkun
    Hasturkun about 13 years
    @zaharpopov: According to the documentation, the non-boost asio does not require Boost unless you use the read_until() or async_read_until() overloads that take a boost::regex parameter.

Related