【问题标题】:How to link C++ program with Boost using CMake如何使用 CMake 将 C++ 程序与 Boost 链接
【发布时间】:2011-04-23 07:23:07
【问题描述】:

将我的程序与 Ubuntu 下的 Boost 库链接时,我的 CMake 文件应该是什么样的?

运行make时显示的错误:

main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'

主文件真的很简单:

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {

    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ;

    return 0;
}

我已经做到了。我添加到 CMake 文件中的唯一行是:

target_link_libraries(
my_target_file
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

【问题讨论】:

    标签: c++ boost cmake


    【解决方案1】:

    在 CMake 中,您可以使用 find_package 来查找您需要的库。通常有一个FindBoost.cmake 以及您的 CMake 安装。

    据我所知,它将与其他常见库的查找脚本一起安装到/usr/share/cmake/Modules/。您可以查看该文件中的文档以了解有关其工作原理的更多信息。

    我脑海中的一个例子:

    FIND_PACKAGE( Boost 1.40 COMPONENTS program_options REQUIRED )
    INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
    
    ADD_EXECUTABLE( anyExecutable myMain.cpp )
    
    TARGET_LINK_LIBRARIES( anyExecutable LINK_PUBLIC ${Boost_LIBRARIES} )
    

    我希望这段代码有所帮助。

    【讨论】:

    • 添加了来自 Kitwares Github 存储库的工作链接。还添加了关于 FindBoost.cmake 的官方文档的链接
    • 为什么要特别提升 1.40?库的基本功能何时停止更改?
    • 因为为什么不呢?这只是一个例子......填写最适合您的内容
    • 一个重要的细节是将target_link_libraries 放在 add_executable find_package 行之后,因此所有链接的组件都是已知的。
    • 这个答案不适合新代码。 oLen的回答应该是首选
    【解决方案2】:

    以下是我的配置:

    cmake_minimum_required(VERSION 2.8)
    set(Boost_INCLUDE_DIR /usr/local/src/boost_1_46_1)
    set(Boost_LIBRARY_DIR /usr/local/src/boost_1_46_1/stage/lib)
    find_package(Boost COMPONENTS system filesystem REQUIRED)
    include_directories(${Boost_INCLUDE_DIR})
    link_directories(${Boost_LIBRARY_DIR})
    
    add_executable(main main.cpp)
    target_link_libraries( main ${Boost_LIBRARIES} )
    

    【讨论】:

    • 不需要链接目录,因为 Boost_LIBRARIES 将是完全限定的路径。
    • 在我的情况下,link_directories 是必要的。
    • 这个答案不适合新代码。 oLen的答案应该是首选
    【解决方案3】:

    为具有导入目标的现代 CMake 语法调整 @MONsDaR 答案,这将是:

    find_package(Boost 1.40 COMPONENTS program_options REQUIRED)
    
    add_executable(anyExecutable myMain.cpp)
    
    target_link_libraries(anyExecutable Boost::program_options)
    

    请注意,不需要手动指定包含目录,因为它已经通过导入的目标Boost::program_options 处理。

    【讨论】:

    • 这种目标方法的缺点是如果您的 Boost 版本比您的 CMake 版本新,这可能会失败。 The FindBoost.cmake 最初仅在明确列出您的 Boost 版本时才构建这些。在某些时候,这应该得到改进,但我仍然看到 CMake 3.10.2 和 Boost 1.66(brew 的最新副本)失败。
    • stackoverflow 开始失败,因为有太多过时的答案,通常很难找到正确的答案(这个)。
    【解决方案4】:

    两种方式,使用系统默认安装路径,一般为/usr/lib/x86_64-linux-gnu/

    find_package(Boost REQUIRED regex date_time system filesystem thread graph)
    include_directories(${BOOST_INCLUDE_DIRS})
    message("boost lib: ${Boost_LIBRARIES}")
    message("boost inc:${Boost_INCLUDE_DIR}")
    
    add_executable(use_boost use_boost.cpp)
    target_link_libraries(use_boost
            ${Boost_LIBRARIES}
            )
    

    如果您将 Boost 安装在 本地目录 或选择本地安装而不是系统安装,您可以这样做:

    set( BOOST_ROOT "/home/xy/boost_install/lib/" CACHE PATH "Boost library path" )
    set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
    
    find_package(Boost REQUIRED regex date_time system filesystem thread graph)
    include_directories(${BOOST_INCLUDE_DIRS})
    message("boost lib: ${Boost_LIBRARIES}, inc:${Boost_INCLUDE_DIR}")
    
    add_executable(use_boost use_boost.cpp)
    target_link_libraries(use_boost
            ${Boost_LIBRARIES}
            )
    

    注意上面的目录 /home/xy/boost_install/lib/ 是我安装 Boost 的地方:

    xy@xy:~/boost_install/lib$ ll -th
    total 16K
    drwxrwxr-x 2 xy xy 4.0K May 28 19:23 lib/
    drwxrwxr-x 3 xy xy 4.0K May 28 19:22 include/
    
    xy@xy:~/boost_install/lib$ ll -th lib/
    total 57M
    drwxrwxr-x 2 xy xy 4.0K May 28 19:23 ./
    -rw-rw-r-- 1 xy xy 2.3M May 28 19:23 libboost_test_exec_monitor.a
    -rw-rw-r-- 1 xy xy 2.2M May 28 19:23 libboost_unit_test_framework.a
    .......
    
    xy@xy:~/boost_install/lib$ ll -th include/
    total 20K
    drwxrwxr-x 110 xy xy  12K May 28 19:22 boost/
    

    如果你对如何使用本地安装的Boost感兴趣,可以看这个问题How can I get CMake to find my alternative Boost installation?

    【讨论】:

      【解决方案5】:

      这是我的看法:

      cmake_minimum_required(VERSION 3.15)
      
      project(TryOuts LANGUAGES CXX)
      
      find_package(Boost QUIET REQUIRED COMPONENTS program_options)
      
      if(NOT Boost_FOUND)
          message(FATAL_ERROR "Boost Not found")
      endif()
      
      add_executable(helloworld main.cpp)
      
      target_link_libraries(helloworld PUBLIC Boost::program_options)
      

      【讨论】:

      • target_link_libraries(helloworld PUBLIC Boost::program_options) 将给出一个 CMAKE 错误: -- 在 CMakeLists.txt:102 (add_executable) 处配置完成的 CMake 错误:目标“DB32”链接到目标“Boost:: program_options”,但没有找到目标。也许 IMPORTED 目标缺少 find_package() 调用,或者缺少 ALIAS 目标?
      • 您好,感谢您的反馈。似乎 CMake 无法在您的系统上找到 Boost。也许你最好从源头重新构建 Boost。在此之前,您可能需要尝试以下操作:使用 find_package(Boost QUIET REQUIRED) 查找包并链接到您的目标 (DB32):target_link_libraries(DB32 PUBLIC Boost::headers)
      • 因为你把REQUIREDNOT Boost_FOUND 永远不会是真的,所以整个if 声明是没有意义的。
      【解决方案6】:

      哪个 Boost 库?其中许多是纯模板,不需要链接。

      现在实际显示的具体示例告诉我们您需要 Boost 程序选项(甚至更多告诉我们您在 Ubuntu 上),您需要做两件事:

      1. 安装 libboost-program-options-dev 以便您可以链接到它。
      2. 告诉cmake 链接到libboost_program_options

      我主要使用 Makefiles 所以这里是直接命令行使用:

      $ g++ boost_program_options_ex1.cpp -o bpo_ex1 -lboost_program_options
      $ ./bpo_ex1
      $ ./bpo_ex1 -h
      $ ./bpo_ex1 --help
      $ ./bpo_ex1 -help
      $
      

      它看起来并没有多大作用。

      对于 CMake,您需要将 boost_program_options 添加到库列表中,而 IIRC 这是通过 CMakeLists.txt 中的 SET(liblist boost_program_options) 完成的。

      【讨论】:

      • 您的 CMake 建议是错误的(请参阅已接受的答案),并且您的命令行建议不是很有帮助,因为问题是关于 CMake。
      猜你喜欢
      • 2018-09-25
      • 1970-01-01
      • 2017-10-18
      • 2019-01-31
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多