Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Asio as header-only

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?

like image 963
zaharpopov Avatar asked Feb 23 '11 09:02

zaharpopov


People also ask

Is Boost ASIO header-only?

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

Is Boost header-only?

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are: Boost.

Is Boost ASIO cross platform?

Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach.

Who uses Boost ASIO?

The systems software for managing an IBM Blue Gene/Q supercomputer uses Boost. Asio extensively.


2 Answers

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."

like image 176
Ralf Avatar answered Sep 19 '22 13:09

Ralf


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...)

like image 23
evadeflow Avatar answered Sep 19 '22 13:09

evadeflow