如何使用cmake与boost asio?

iezvtpos  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(203)

我不能让boost::asio在我的c程序中与cmake一起工作。我实际上已经尝试和谷歌了很多个小时,但是我不能让它工作!
我想在Ubuntu 18.04下的C
项目中包含一个cmake文件boost::asio
因此,我安装了最新的CMake(cmake version 3.19.4),下载了boost版本1.74并执行

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

安装目录为/home/boost/boost_1_74_0。我的CMake文件如下所示:

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)

但我得到的一切都是如下:

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

我到底做错了什么?难道CMake没有告诉我它找到了Boost 1.74吗?如果我尝试find_package(Boost REQUIRED),CMake * 不会 * 抛出任何错误,但链接也不起作用。我明确告诉CMake在哪里找到库,那么为什么CMake找不到Boost?

mrwjdhj3

mrwjdhj31#

试试这个。

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})
daolsyd0

daolsyd02#

我刚刚遇到了一个类似的问题,发现我错过了Boost安装中一个很小但很重要的步骤:将安装目录(即b2 install --prefix=PREFIX中使用的PREFIX)添加到PATH环境变量中。
对我来说,这解决了它-希望对你来说也是!

相关问题