【问题标题】:How can I find installed Boost library on ubuntu using CMake?如何使用 CMake 在 ubuntu 上找到已安装的 Boost 库?
【发布时间】:2017-08-28 20:19:16
【问题描述】:

我已经使用这个命令安装了 Boost

sudo apt-get install libboost-all-dev

我在 main.cpp 中写了这个简单的例子

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.wait();

  std::cout << "Hello, world!" << std::endl;

  return 0;
}

在我的 CMakeLists.txt 我有这个:

cmake_minimum_required(VERSION 2.8)

find_package(Boost REQUIRED)
if(NOT Boost_FOUND)
    message(SEND_ERROR "Failed to find Boost")
    return()
else()
    include_directories(${Boost_INCLUDE_DIR})
endif()

add_executable(main main.cpp)

CMake 工作正常,但使用 make 启动后出现一些错误:

main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'

如何在我的 CMakeLists.txt 中正确包含 boost 以便 cmake 找到库?

【问题讨论】:

标签: c++ linux boost cmake


【解决方案1】:

您需要链接到 boost 库。 FindBoost 为此提供了变量Boost_LIBRARIES

add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES})

有关详细信息,请参阅the FindBoost documentation。结尾有一个例子。

【讨论】:

    【解决方案2】:
    main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
    

    链接步骤失败。您没有链接到系统库。你需要这样做。

    关于 CMake 使用 boost,您没有遇到任何错误。您只需要告诉它需要链接系统即可。

    【讨论】:

      【解决方案3】:

      要补充之前的答案,here 是您需要链接的 Boost 库列表。 (从 Boost 1.65 开始)

      只需包含标题即可使用所有其他 boost 库。

      【讨论】:

        猜你喜欢
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        • 2020-03-01
        • 2011-03-02
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 2019-08-25
        相关资源
        最近更新 更多