-1

This is how my CMakeLists.txt looks like

I downloaded the asio library from here. It consists of many header files that you have to include in your project. The problem is that those headers are inside directories and the #include statements just don't match the correct directory.

For example, they have a asio/async_result.hpp file. That file includes asio/detail/config.hpp. However it should be ../asio/detail/config.hpp

What am I supposed to do ? Single handily rename all those 200 header files to fit the correct directory structure ? Why are they wrong out of the box ?

1

Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • Just a note to future readers: `asio` is now part of `boost::asio`. Don't install it isolatedly, but get it from boost. – Marcus Müller Apr 09 '16 at 13:35
  • The include directives within Asio require that Asio is available in the include path. Does adding the `server_client` directory to the include path via `include_directories()` to your CMakeLists.txt file resolve the problem (e.g. `include_directories(${CMAKE_CURRENT_SOURCE_DIR})`? – Tanner Sansbury Apr 11 '16 at 14:43
  • @TannerSansbury I am writing an answer to this question – dimitris93 Apr 11 '16 at 15:10
  • 3
    @MarcusMüller Standalone asio is still present, and is being developed to become part of the standard library, so I feel that in the near future the standalone version will be a better choice. – lisyarus Apr 11 '16 at 15:19
  • @lisyarus knowing multiple projects that have been using `boost::asio` for years now, I'd say that for anyone who doesn't need the new development, using `boost::asio` is probably preferable (especially since Boost is used by so many C++ projects, anyway). – Marcus Müller Apr 11 '16 at 15:49
  • 2
    @MarcusMüller If you're not using other parts of Boost, the standalone is probably preferable. The standalone version is header-only, but the Boost version is not - that alone is reason enough to prefer it if you don't already have to link with any Boost libraries. – Ethan May 05 '17 at 01:31
  • @ethan thanks! I've highlighted your comment; hope it's useful – Marcus Müller May 05 '17 at 06:42

3 Answers3

2

You have to install these headers first. On most systems (aside from windows, of course), there's a typical path where such include files end up.

It's the job of your project's build system (e.g. Cmake) to find asio on your computer and configure the compiler so, that it looks in those folders, and finds the right files if you do

#include <asio/handler_type.hpp>

Do never create your project within the libary you're using (unless, of course, you want to change/extend that library). Create a project somewhere else and tell cmake to look for asio.

Also, unless your source code is part of the library itself, you should never include _detail.hpp files; these are typically not part of the API that users of a library make use of, but internal structures.

EDIT

As noted above, asio is now part of boost, so install boost including its boost::asio module correctly, instruct cmake to

 find_package(Boost COMPONENTS asio)

and use for example

 #include <boost/asio.hpp>
 #include <boost/thread/thread.hpp>

Refer to the official Boost asio Tutorial for more examples.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • How exactly do I install headers ? – dimitris93 Apr 09 '16 at 13:30
  • @Shiro copy them into the right directory. Usually, a library comes with scripts and instructions on installing. – Marcus Müller Apr 09 '16 at 13:31
  • By the way, today, one woutld use `boost::asio` instead of `asio`, because `asio` has become included with boost quite some time ago, and is continuedly developed within the boost community. – Marcus Müller Apr 09 '16 at 13:32
  • … and every OS that I can think of either has an "internal" way of installing boost including `boost::asio`, or an installer – Marcus Müller Apr 09 '16 at 13:33
  • I don't know how to install Boost... Here is my [question](http://stackoverflow.com/questions/36519453/setup-boost-in-clion) – dimitris93 Apr 09 '16 at 16:48
  • 3
    Most development occurs in the Asio library, then gets ported to Boost.Asio. Non-Boost Asio is an option if one wishes to not introduce a dependency on Boost, use features that have not yet landed into Boost.Asio, or wishes to experiment with Networking TS changes. – Tanner Sansbury Apr 11 '16 at 14:21
  • @TannerSansbury true! But assuming OP really wants to use something that has a stable API rather than a relatively actively developing library, I was under the impression that the "every OS has its easy method to get Boost" approach would be preferrable here. – Marcus Müller Apr 11 '16 at 15:43
  • 1
    For what it is worth, my experience is that the stable releases for non-Boost Asio provide as much as a stable API as the stable releases for Boost.Asio. – Tanner Sansbury Apr 11 '16 at 16:49
2

here's a simple CMake script that builds the example HTTP client, using ASIO standalone, set to generate a Visual Studio 2019 project with

cmake -H. -Bbuild -G "Visual Studio 16 2019" -A x64 

https://think-async.com/Asio/AsioStandalone.html

(no boost). Assumes source in folder asio-1.12.2

cmake_minimum_required(VERSION 3.1)
project (asio)

set(CMAKE_CXX_STANDARD 11)
add_definitions(-DASIO_STANDALONE)
add_definitions(-DASIO_HAS_STD_ADDRESSOF)
add_definitions(-DASIO_HAS_STD_ARRAY)
add_definitions(-DASIO_HAS_CSTDINT)
add_definitions(-DASIO_HAS_STD_SHARED_PTR)
add_definitions(-DASIO_HAS_STD_TYPE_TRAITS)
add_definitions(-DASIO_HAS_VARIADIC_TEMPLATES)
add_definitions(-DASIO_HAS_STD_FUNCTION)
add_definitions(-DASIO_HAS_STD_CHRONO)
add_definitions(-DBOOST_ALL_NO_LIB)
add_definitions(-D_WIN32_WINNT=0x0501)
add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS)

include_directories(asio-1.12.2/include)
add_executable(sync_client asio-1.12.2/src/examples/cpp11/socks4/sync_client.cpp)
Pedro Vicente
  • 681
  • 2
  • 9
  • 21
-1

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
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 2
    This is kind of bad style; it's usually vastly preferable to use an existing CMake "Find..." module to set the include and linker options, or to write one yourself; like this, you really gain very little be employing cmake. Again, for standalone asio, there's no "standard" cmake Module to do thins, ([but googling finds some that you'd only have to amend with a few hints for windows](http://www.na-mic.org/svn/NAMICSandBox/WFEngine/FindAsio.cmake)), but for `boost::asio`, you get well-tested, robust Library finding routines. – Marcus Müller Apr 11 '16 at 15:47
  • 2
    Downvoting for the "I admit I don't know what I'm doing" `#define` here: you arbitrarily set your [windows version constant](https://msdn.microsoft.com/en-us/library/6sehtctf.aspx) to Windows XP, with unforeseen side-effects. Hard-coding your OS and OS version into your code is rendering it non-portable. Again, you should be detecting your Windows version in CMake and set it appropriately. – Marcus Müller Apr 11 '16 at 15:56
  • @MarcusMüller You are saying that the `-I` flag is a bad way of including a header-only library to my project ? In my IDE, Clion, the library is recognizes the library perfectly. What exactly are you suggesting instead ? As far as the `_WIN32_WINNT` version goes, I believe that the windows XP version is simply the minimum, so using that will run to a windows XP machine and above. That's the impression I got from a `warning` I got when compiling without that flag defined. – dimitris93 Apr 11 '16 at 17:13
  • @MarcusMüller Also, the header-only library is much more portable than boost library. Who wants to go through the process of installing extra libraries when you can copy and paste the 2MB header library of `asio` and then do `-I asio` when compiling with `g++` in your linux machine ? If anything, boost will just take up a lot of disk space and do the same exact job plus will take more time to install. That's the point of the library being *header-only* – dimitris93 Apr 11 '16 at 17:14
  • `boost::asio` is just as header-only as standalone `asio`. And regarding "who wants to install a full boost if he can get a header only asio"? Someone not too experienced with build tools, maybe. Especially, I was thinking of you :) That's why I recommended it. You're happier with standalone `asio`, so go for it. I still don't recommend it. – Marcus Müller Apr 11 '16 at 18:33
  • @MarcusMüller Yesterday I spent about 12 hours trying to make `asio` work. I even tried installing boost, if you look at my other questions. Didn't manage to make it work. Boost is a pain to get it working for not gurus. – dimitris93 Apr 11 '16 at 18:57
  • @MarcusMüller And as a sidenote I want to say that I am not using Cmake because I like it. I am stuck with it because of my IDE Clion. So the `-I` command makes sense because its the same exact command line on linux. Its so easy to use. And also this code compiles fine just with mingw `g++` if you want to compile it from command line, just like linux – dimitris93 Apr 11 '16 at 19:02