Boost.Asio as Header-Only

Boost.Asio as header-only

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

Header-only asio standalone

As noted on the Asio website:

When using a C++11 compiler, most of Asio may now be used without a dependency on Boost header files or libraries. To use Asio in this way, define ASIO_STANDALONE on your compiler command line or as part of the project options.

Thus even when ASIO_STANDALONE is defined, Asio will use Boost when:

  • Using a non-C++11 compiler.
  • When using certain features, such as stackful coroutines that are based on the Boost.Coroutine library.

With asio-1.10.2, the following program:

#include <asio.hpp>

int main()
{
asio::io_service io_service;
}

compiles with gcc 4.8.1, using -DASIO_STANDALONE -std=c++11 compiler flags. Without specifying the compiler to use c++11, compilation fails when attempting to include Boost header files.

Using asio standalone headers library

Here is my working updated CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(server_client)

# take a look at the -I command
# this will incude all the header files in your project. So simple
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I C:/Users/Shiro/Desktop/asio-1.10.6/include")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -static")
# Don't forget this! The libws2_re.lib library is 100% required for windows !
# I also added libwsock32.lib, not sure if thats necessary
# Those .lib files are located at C:\MinGW\lib on my machine
# That directory is located automatically, you don't have to add the full path
link_libraries(ws2_32 wsock32)

set(SOURCE_FILES chat_server.cpp)
add_executable(server_client ${SOURCE_FILES})

On my your source .cpp file you have to add

// this is required otherwise asio tries to include 
// other boost libraries which you won't have installed
// or you could add a -DASIO_STANDALONE flag right next to -std=c++11 in CMakeLists.txt
#define ASIO_STANDALONE

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 // windows stuff, not sure what this does
#endif

#include <asio.hpp> // include asio library

Boost.Asio without Boost.System

AFAIR you can configure Boost System to be header-only

Sample Image

Source: http://www.boost.org/doc/libs/1_66_0/libs/system/doc/reference.html

Other than that, you might simply use Non-Boost Asio

Is it possible to use Boost serialization as a header only library?

I ended up including the cpp sources from a certain version of Boost Serialization.
I chose the cpp files by trial and error.

https://gitlab.com/correaa/boost-mpi3/-/tree/master/include/mpi3/serialization_hack

serialiation_hack

Basically, I include these cpp files from the same place I would include the Serialization hpp files.

#include <boost/archive/detail/common_iarchive.hpp>
#include <boost/archive/detail/common_oarchive.hpp>

#include <boost/archive/archive_exception.hpp>
#include <boost/archive/basic_streambuf_locale_saver.hpp>
#include <boost/archive/detail/auto_link_archive.hpp>
//#include <boost/archive/detail/abi_prefix.hpp> // must be the last header

#include <boost/serialization/array.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
#include <boost/serialization/item_version_type.hpp>
#include <boost/serialization/string.hpp>

#include <boost/mpl/placeholders.hpp>

#include <any>
#include <optional>

// use this to avoid need for linking -lserialization
#ifdef _MAKE_BOOST_SERIALIZATION_HEADER_ONLY
//#include <boost/archive/detail/decl.hpp>
#if BOOST_VERSION > 106000 && BOOST_VERSION < 106600
#include "../mpi3/serialization_hack/singleton.cpp"
#endif
#if BOOST_VERSION < 105900
#define BOOST_ARCHIVE_DECL
#define BOOST_SERIALIZATION_DECL
#endif
// NOLINTBEGIN(hicpp-use-auto,modernize-use-auto) external code
#include "../mpi3/serialization_hack/archive_exception.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/basic_archive.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/basic_iarchive.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/basic_iserializer.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/basic_oarchive.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/basic_oserializer.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/extended_type_info.cpp" // NOLINT(bugprone-suspicious-include) hack
#include "../mpi3/serialization_hack/extended_type_info_typeid.cpp" // NOLINT(bugprone-suspicious-include) hack
// NOLINTEND(hicpp-use-auto,modernize-use-auto)

A problem with this approach is that I had to modify the sources to accommodate different versions of Boost.Serialization and also had to do some modification to appease compiler warnings and static analyzers.

Making a header file of the Boost::Asio chat_server example gives me one or multiple defined symbols found error

See https://github.com/chriskohlhoff/asio/pull/584

include/boost/asio/impl/use_awaitable.hpp has an error, dummy_return should be marked inline.

Change:

template <>
void dummy_return()
{
}

to:

template <>
inline void dummy_return()
{
}

Boost asio library for networking (http client)

The Boost.Asio doc:

By default, Boost.Asio is a header-only library. 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.

So, by default Asio is header-file-only and for most uses does not require linking against any Boost library.

But, boost Asio does depens on other boost components likt Boost.System, Boost.Thread(not nessesary) etc. to support better async operations and some error codes ( boost::system::error_code and boost::system::system_error).

see here and here for more info.

How to use precompile header with boost::asio

If your precompiled header is called PCH.H you also should have a PCH.CPP file that gets compiled with /Yc and linked (as PCH.OBJ) to all the others that you compile with /Yu.

For the other cases that work, perhaps this is because nothing static from the Boost headers has to get exported from the precompiled header object file.



Related Topics



Leave a reply



Submit