Boost asio library for networking (http client) - c++

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.

Related

Why does this need the Boost lib file?

I'm using the ASIO libraries to make a udp sending wrapper. The intent is for this to be used by another app to easily send 3 specific udp messages.
I've created a .lib file which is basically an exported utility class that wraps the ASIO functions.
To test my lib I also made a little command line app which links to my lib, creates the exported class and calls the send function.
However, the test application is requiring to link to libboost_system-vc100-mt-gd-1_55.lib but the lib file I created which actually contains the Boost code does not.
Why is this happening and how can I fix this?
A .lib file, static library, is just a grouping of object files, it's not an executable entity. It isn't linked thus it doesn't require it's unresolved symbols to be resolved.
Only the executable or shared library (DLL) that link with it need the dependencies (in this case your test code).
So there's no problem, perhaps you meant to bundle your library as a shared library rather than a static library?
Most of the boost libraries rely on boost::system because of the exception/error management is used.
boost::asio definitely uses that.
A test runner application needs to link everything as it's going to be a (statically or dynamically linked) executable, and all of the references need to be resolved.
The Boost::Asio library is available (from the same author) as a header-only standalone version.
See his think-async.com website for details, and comparison. The standalone version is useful when you do not need (or want) to have a link-time depedency on Boost. More details are on the AsioStandalone page.
FWIW I bundled this for use by R programmers as CRAN package AsioHeaders because the 'no linking' feature makes the cross-platform use particularly appealing.
You could similarly provide header-only solution for your application.

How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?

For any C++ Boost library, how can one find out which Boost library(ies) it requires ?
Example (not necessary a working example though): Boost library "test" requires Boost library "date_time".
Regards,
boost comes with a tool to gather the dependencies of a library.
It is called bcp. If you just want a list of files, you have to use the --list option.
If you want to find out those dependencies to isolate the components your software requires, you can use bcp (Boost Copy)
It copies selected boost libraries and all its dependencies to a target location.
Eg
bcp regex /foo
copies the complete regex library and its dependencies to /foo
Disclaimer: I do not have any practical experience with bcp.
EDIT:
If you only want to check on which compiled library a compiled library depends, you can either use ldd <boost_library_filename>.so on Linux or Dependency Walker on Windows.
A modern solution is to use boost
Dependency Report (available starting from boost v1.66.0).

How I use only Asio of Boost Library?

how I use only the Asio of Boost Library without adding any other library? Is that possible?
In the Boost, don't have the folder called "lib" with the *.a files?
Asio exists as as standalone library, entirely independent of Boost (this is the "original").
No need to search together the bits from Boost subdirectories if you don't want to use Boost, just download Asio and use that. :-)
Since Asio is a header-only library, there are no lib files associated with it. You can extract only the absolutly needed header files with bcp, which is included.

boost & asio as part in big project with Cmake

I want to use Boost core libraries (like lambda) and Boost ASIO library in a large cross-platform project built with the Cmake. I want to put Boost & ASIO in my source control tree and build it as part of project with the Cmake. So I don't want to "install" it on computers and link with it.
How can this be done? Does there exist CMakeLists.txt for Boost?
Both Boost.Lambda and Boost.ASIO are header-only libraries, so in your CMakeLists.txt file, you could include your Boost include directory in the INCLUDE_DIRECTORIES variable, and when your code is built, the header code will be built into your resulting modules.
For situations where you are using non-header-only libraries, like Boost.Filesystem, then you will have to either ship libraries you built, or modify the experimental CMake version of boost.build, which you can find here: https://svn.boost.org/trac/boost/wiki/CMake
Edit
I was slightly confused about Boost.Asio. It is not truly header-only, because it depends on Boost.System, and possibly Boost.Thread depending on how you use it.
However, Boost.Asio is derived from Asio ( http://think-async.com/ ), which is header-only.
There is a project aiming at providing a CMake version of boost. It is quite outdated, but the CMakeLists.txt might help you. You can check it out there.

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