4

I cannot get boost::asio to work with cmake in my c++ program. I have actually tried and googled for many hours, but I cannot get it to work!

I want to include boost::asio in my c++ Project under Ubuntu 18.04 with a cmake file.

So I installed the newest CMake (cmake version 3.19.4), and I downloaded boost version 1.74 and executed

./bootstrap.sh --prefix=/usr/
sudo ./b2 install

The install directory is /home/boost/boost_1_74_0. My CMake file looks like this:

cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)

# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR  "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components asio) 
    
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)

But everything I get is the following:

vm-umic@vm:~/Projects/tryout/build$ cmake ..
CMake Warning at /snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2034 (message):
  No header defined for asio; skipping header check (note: header-only
  libraries have no designated component)
Call Stack (most recent call first):
  CMakeLists.txt:27 (find_package)


CMake Error at /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
  Could NOT find Boost (missing: asio) (found version "1.74.0")
Call Stack (most recent call first):
  /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
  /snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2193 (find_package_handle_standard_args)
  CMakeLists.txt:27 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/vm-umic/Projects/tryout/build/CMakeFiles/CMakeOutput.log".

What in the world am I doing wrong? Isnt CMake telling me that it found Boost 1.74? CMake does NOT throw any errors if I try find_package(Boost REQUIRED), but then linking does also not work. I explicitly tell CMake where to find the libraries, so why can't CMake finde Boost?

stark
  • 12,615
  • 3
  • 33
  • 50
U_flow
  • 475
  • 5
  • 18

2 Answers2

2

Try this.

cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)

# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR  "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components system) 
    
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
target_link_libraries(${PROJECT_NAME}
    ${Boost_LIBRARIES})
Paul Varghese
  • 1,635
  • 1
  • 15
  • 30
  • 9
    Code-only answers aren’t nice. Please explain *why* this fixes the problem. – Kuba hasn't forgotten Monica Feb 02 '21 at 02:43
  • I tried this, unfortunately this does not work as intended, it tells me: `CMake Error at /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message): Could NOT find Boost (missing: Components system) (found version "1.74.0")` Perhaps I did not correctly install boost? How can it find boost but not this library? – U_flow Feb 02 '21 at 08:12
  • 2
    To answer @Kubahasn'tforgottenMonica 's comment, it should work because although `boost asio` is header only, it depends on `boost system` for error handling so it links to the `boost system` binary. @U_flow it sounds like you haven't built `boost`. You can build it by following the instructions here: https://www.boost.org/doc/libs/1_75_0/more/getting_started/unix-variants.html or even easier by just installing it, see: https://stackoverflow.com/questions/12578499/how-to-install-boost-on-ubuntu. Note, Ubuntu is likely to install an older version than 1.74 or 1.75 (the latest). – kenba Feb 02 '21 at 08:34
  • 2
    The case of `find_package` keyword `COMPONENTS` matters here. The error message lists `Components` as missing library. – vre Feb 02 '21 at 08:58
  • Btw, you are missing a call to `target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})` – vre Feb 02 '21 at 09:13
  • @kenba perhaps you are right and I havent't (successfully) built boost! I just reread the documentation on building and the "lib" folder should be built (although I have a "libs" (with an s) folder). I have not used apt-get install, because it threw me a bunch of "uninstalled dependencies errors" so I thought it would be easier to install directly from source, but I guess some of the dependencies are needed... – U_flow Feb 02 '21 at 16:28
1

I just had a similar problem, and found that I had missed a small but important step in the Boost installation: adding the installation directory (i.e. the PREFIX used in b2 install --prefix=PREFIX) to the PATH environment variable.

For me, this solved it – hopefully will for you too!

Oded R.
  • 475
  • 1
  • 4
  • 10