【问题标题】:Using Boost.asio with cmake?将 Boost.asio 与 cmake 一起使用?
【发布时间】:2013-03-08 09:12:01
【问题描述】:

我想在没有外部库的情况下将 boost.asio 静态链接到我的小项目(结果只有一个 exe/bin 文件来分发它)。 Boost.asio 需要 Boost.system,我开始沉浸在试图弄清楚如何编译这一切的过程中。 如何在 cmake 中使用 Boost.asio?

【问题讨论】:

  • 所有boost组件都可以静态链接。你又是什么问题?
  • 我用谷歌搜索了很多次,每次我发现一些问题为什么我不能编译它。所以我提出了如何准确地做到这一点的问题。我无法使用 cmake 编译 boost 的任何部分,也不知道如何在我的项目中使用 cmake 静态地使用它。
  • 看看FindBoost.cmake的内容。您可以在 CMake 安装的Modules/ 目录中找到它。

标签: c++ boost cmake boost-asio


【解决方案1】:

如果我理解实际问题,它基本上是在询问如何在 CMake 中静态链接到 3rd 方库。

在我的环境中,我已将 Boost 安装到 /opt/boost

最简单的方法是使用 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})

查找所有 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})

如果您没有正确安装 Boost,则有两种方法可以静态链接库。第一种方法创建一个导入的 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)

另一种方法是在target_link_libraries 中显式列出库而不是目标:

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

【讨论】:

    猜你喜欢
    • 2016-03-05
    • 1970-01-01
    • 2015-04-08
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2019-12-31
    相关资源
    最近更新 更多