Boost.Asio as header-only - c++

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?

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

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

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.

Related

Use boost::asio without linking to OpenSSL

I'm working on a project that uses boost::asio. By default it links with OpenSSL libraries, however no SSL features are used. I need to get rid of OpenSSL dependencies, is there a way to do it? Thanks
Your question doesn't really make sense as the OpenSSL part of boost asio is optional. i.e. it only links in openssl when you use it.
Also by default boost asio is header only. So you must have gone out of your way to compile the library version WITH openssl included.
You can read about it here.
If you want to only include openssl when you use it, then I would just use the default header only version and you will only get what you use and nothing more.
Compile boost yourself and remove the OpenSSL bits first. Would be "a way to do it".

Boost asio library for networking (http client)

I'm trying to compile a utility in C++ with Visual Studio 2015, which requires the Boost asio library for networking (http requests).
The same solution includes a project to build this Boost asio library, but I had to download the boost_1_59_0.7z file. So far, I was able to download it and built was good, but I found the build generated like 11 libs with different names (libboost_chrono-vc140-mt-sgd-1_59.lib, and 10 more but with date_time, filesystem, log-setup, etc...).
On the other hand, the project that requires this lib is looking for a library named boost_1_59_0.lib, so my question is how do I know which of the generated libraries is the right one to use for networking (I don't see any name related with asio or networking...).
I would appreciate any guidance on this.
Thanks
Gus
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.
boost asio is a header-only library but asio networking depends upon boost system for error messages, which isn't a header only library.
In short, your code needs to link to boost-system and it may also require boost-chrono for asio timers.
Note: boost supports autolinking in Visual Studio; so you just need to add the path to the boost library directory to the Library Directories configuration properties, e.g.:
$BOOST_ROOT\stage\lib
Where $BOOST_ROOT is the directory where you installed boost.
You do not need to add boost-system or boost-chrono to Linker->Input->Additional Dependencies. However, you may need to add Windows networking libraries, such as: wsock32 and ws2_32.

Can we use BOOST_SPIRIT_THREADSAFE flag always when read_json is used in multiple threads without linking with boost.thread libraries?

We are using boost in our project. We are not linking any boost libraries but we are including boost header files like boost/property_tree/ptree.hpp.
We are calling read_json from multiple threads (not boost threads but posix threads) and we are getting crashes at read_json() function. Now we included BOOST_SPIRIT_THREADSAFE before including header files as boost json parser is not thread safe and every thing is working fine. But our reviewer is not accepting this change and he is pointing to the below link
http://www.boost.org/doc/libs/1_60_0/libs/spirit/classic/doc/grammar.html
As this page mentioned "On the other hand, if a grammar
is intended to be used in multithreaded code, we should then define
BOOST_SPIRIT_THREADSAFE before including any spirit header files. In this
case it will also be required to link against Boost.Threads"
But do we really need to link with the Boost.Threads library as we are not using boost threads and my understanding is boost threads internally will use posix threads on Linux platform. Can any one please let me know if I am wrong.
The reviewer is linking to "1.60.0" documentation... of Classic Spirit.
Spirit classic has been obsolete for a decade or more.
What's more, Boost Property Tree has rewritten it's parsers: it doesn't use Spirit at all in 1.60.0. This has been the case for some versions.
Note there can be issues when using Property Tree outside the main entry-point, see e.g.:
boost::property_tree::info_parser breaks on spaces in value

Is there a way to get Asio working without Boost?

I know there is a version of ASIO that is not included in the Boost namespace, but even then ASIO depends on Boost, but I'm wondering if there is a way to get ASIO to work without dependencies on Boost (because I cannot include Boost into the project, for too many reasons).
No, i don't believe so. ASIO has been using boost for as long as i have heard of it. I think they're very much interconnected. But you may be interested in a tool, bcp, which lets you extract the minimal subset of boost required for the libraries that you want to use.
There is also a non-boost version of Asio:
Asio comes in two variants: (non-Boost) Asio and Boost.Asio.
See:
http://think-async.com/Asio/
The "non-boost asio" has its own thread bits instead of using boost.thread, but it still requires boost.date_time, boost.array, boost.utility, boost.bind, boost.shared_ptr...
There is no version of Asio that can work without any Boost dependency.
Recent ASIO versions can work without Boost in a standalone mode. This mode was probably enabled by the C++11 support of the library because it can now use standard threads, futures, etc instead of their Boost implementation. You can simply include ASIO like below and enjoy:
#define ASIO_STANDALONE
#include <asio.hpp>

Using boost shared_ptr

I have to use a smart pointer and I found "shared_ptr" from boost looks good. I downloaded the boost library and I can see many files and libraries there. Is there any way to use only the shared_ptr ?
boost bcp is your friend. It allows extracting individual boost libraries out of its tree. I used it with success in the past. shared_ptr consists only of headers, so that will be especially good for you.
You can use bcp as litb suggested, but if you're worried about dragging in extra library code, you shouldn't be. Boost, in general, follows the C++ philosophy of "you only pay for what you use". So, if you include only the shared_ptr headers, that's all that your code will use.
If you're using a recent version of Visual C++ on Windows, BoostPro provides a convenient free installer here: http://www.boostpro.com/products/free.
Otherwise, or if you have already downloaded the source distribution, you should in fact be able to start using shared_ptr and friends right away as the shared_ptr library is "header-only" -- no compilation of .cpp files is required.