如何在Boost Asio上使用cmake? [英] Howto use cmake with boost asio?

查看:240
本文介绍了如何在Boost Asio上使用cmake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的c ++程序中使 boost :: asio 与cmake一起使用.我实际上已经尝试和搜索了多个小时,但是我无法使它正常工作!

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!

我想在Ubuntu 18.04下的c ++项目中使用cmake文件包含 boost :: asio .

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

所以我安装了最新的CMake(cmake版本3.19.4 ),然后下载了boost版本1.74并执行了

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

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

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

我到底在做什么错?CMake不是告诉我找到了Boost 1.74吗?如果我尝试 find_package(需要提升功能),CMake不会不会引发任何错误,但是链接也不起作用.我明确告诉CMake在哪里可以找到库,那为什么CMake找不到Boost?

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?

推荐答案

尝试一下.

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

这篇关于如何在Boost Asio上使用cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆