简体   繁体   English

使用Boost.asio和cmake?

[英]Using Boost.asio with cmake?

I want to static link boost.asio to my small project without external libs (having only single exe/bin file in result to distribute it). 我想静态链接boost.asio到我没有外部库的小项目(在结果中只有单个exe / bin文件来分发它)。 Boost.asio requires Boost.system and i start to drown trying to figure out how to compile this all. Boost.asio需要Boost.system,我开始淹死试图弄清楚如何编译这一切。 How to use Boost.asio with cmake? 如何在cmake中使用Boost.asio?

If I understand the actual question, it is fundamentally asking how to statically link against 3rd party libraries in CMake. 如果我理解实际的问题,它从根本上询问如何在CMake中静态链接第三方库。

In my environment, I have installed Boost to /opt/boost . 在我的环境中,我已经将Boost安装到/opt/boost

The easiest way is to use FindBoost.cmake provided in a CMake installation: 最简单的方法是使用CMake安装中提供的FindBoost.cmake

set(BOOST_ROOT /opt/boost)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS system)

include_directories(${Boost_INCLUDE_DIR})
add_executable(example example.cpp)
target_link_libraries(example ${Boost_LIBRARIES})

A variant that finds all Boost libraries and explicitly links against the system library: 查找所有Boost库并显式链接系统库的变体:

set(BOOST_ROOT /opt/boost)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED)

include_directories(${Boost_INCLUDE_DIR})
add_executable(example example.cpp)
target_link_libraries(example ${Boost_SYSTEM_LIBRARY})

If you do not have a proper Boost installation, then there are two approaches to statically link against the libraries. 如果没有正确的Boost安装,则有两种静态链接库的方法。 The first approach creates an imported CMake target: 第一种方法创建导入的CMake目标:

add_library(boost_system STATIC IMPORTED)
set_property(TARGET boost_system PROPERTY
  IMPORTED_LOCATION /opt/boost/lib/libboost_system.a 
)

include_directories(/opt/boost/include)
add_executable(example example.cpp)
target_link_libraries(example boost_system)

And the alternative is to explicitly list the library in target_link_libraries rather than the target: 另一种方法是在target_link_libraries而不是目标中明确列出库:

include_directories(/opt/boost/include)
add_executable(example example.cpp)
target_link_libraries(example /opt/boost/lib/libboost_system.a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM