How to run boost asio with cmake? Havint this simple project layout:
c1
├── c1
│ └── main.cpp
└── CMakeLists.txt
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.20.0)
project(c1)
add_executable(c1 c1/main.cpp)
include_directories(.)
set(BOOST_ROOT /usr/local/boost_1_78_0)
find_package(Boost 1.78.0 REQUIRED COMPONENTS system)
target_include_directories(c1 PUBLIC ${Boost_INCLUDE_DIR})
target_link_libraries(c1 LINK_PUBLIC ${Boost_LIBRARIES})
and main.cpp
:
#include <iostream>
#include <boost/asio.hpp>
int main(){
boost::asio::io_context io;
boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
t.wait();
std::cout << "Hello world!" << std::endl;
}
I am getting this cmake error:
CMake Error at /usr/local/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Boost (missing: system) (found suitable version "1.78.0",
minimum required is "1.78.0")
Call Stack (most recent call first):
/usr/local/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
/usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
CMakeLists.txt:8 (find_package)
I am not sure if I understand it correclty, but it is trying to find suitable version 1.78.0
, but it found 1.78.0
, so what is the problem?
Also before that, there was a warning:
CMake Warning at /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1369 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
/usr/local/share/cmake-3.22/Modules/FindBoost.cmake:1492 (_Boost_COMPONENT_DEPENDENCIES)
/usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2102 (_Boost_MISSING_DEPENDENCIES)
CMakeLists.txt:8 (find_package)
So what should I do?
EDIT: the system library is at:
/usr/local/boost_1_78_0/boost/system
and the debug output of where is cmake looking:
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2053 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = "/usr/local/boost_1_78_0/lib;/usr/local/boost_1_78_0/stage/lib;/usr/local/boost_1_78_0/lib;/usr/local/boost_1_78_0/../lib;/usr/local/boost_1_78_0/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2054 ] _boost_LIBRARY_SEARCH_DIRS_DEBUG = "/usr/local/boost_1_78_0/lib;/usr/local/boost_1_78_0/stage/lib;/usr/local/boost_1_78_0/lib;/usr/local/boost_1_78_0/../lib;/usr/local/boost_1_78_0/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib"
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2239 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-gcc10-mt-1_78;boost_system-gcc10-mt;boost_system-gcc10-mt;boost_system-mt-1_78;boost_system-mt;boost_system-mt;boost_system-mt;boost_system
-- [ /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2294 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-gcc10-mt-d-1_78;boost_system-gcc10-mt-d;boost_system-gcc10-mt-d;boost_system-mt-d-1_78;boost_system-mt-d;boost_system-mt-d;boost_system-mt;boost_system
Yet I cannot understand, why the cmake cannot find that library.
missing: system
, which means that CMake cannot find the library corresponding to the "system" component. Run CMake with additional-DBoost_DEBUG=ON
, that way CMake will print which exact libraries it searches and where. Compare that output with files which you actually has. – Spiceberry/usr/local/boost_1_78_0/boost/system
contains a headers. But a library is a.so
file, not headers. Debug output "Searching for SYSTEM_LIBRARY_RELEASE" lists possible names of the file corresponding to the Boost system library, and "_boost_LIBRARY_SEARCH_DIRS_RELEASE" lists directories where this file is searched. Do you have the library file in one of this directories? – Spiceberry/usr/local/boost_1_78_0/libs/system
, and from the output, I see the cmake is looking for the system in_boost_LIBRARY_SEARCH_DIRS_RELEASE = "/usr/local/boost_1_78_0/lib
... So there is libs directory, but the cmake is looking after only "lib" (without s at the end), is this the reason? If so, is not that bug for boost or cmake? Why is there naming inconsistency? – Fairleadlibs
looks like a weird place for Boost libraries. But what about the library itself, which exact path it has? E.g. on Ubuntu the library path is/usr/lib/x86_64-linux-gnu/libboost_system.so
. – Spiceberrymv
ed to/usr/local
,tar -xf <boosttar>
and that's how the directory was created. Then simple in my project, modified theBOOST_ROOT
in the cmake (as you can see from the cmakelist dump above) to point to that path. And yes, triggeredsudo ./bootstrap.sh
from the boots dir. So there is no dpkg involved, the boost is manually installed. But that should not be problem or? – Fairleadsystem
is a header-only library since Boost 1.69, but it can have a library file too. You either need to remove COMPONENTS fromfind_package
call, so downloaded Boost will work. Or you need to install Boost (./bootstrap.sh
and./b2 install
), and tell CMake to use installed variant. By default, Boost is installed into/usr/local
, which is searched by CMake automatically (no needs to set BOOST_ROOT variable). – Spiceberry