Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto use cmake with boost asio?

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?

like image 838
U_flow Avatar asked Oct 29 '25 13:10

U_flow


2 Answers

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})
like image 93
Paul Varghese Avatar answered Oct 31 '25 05:10

Paul Varghese


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!

like image 23
Oded R. Avatar answered Oct 31 '25 05:10

Oded R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!