如何使用CMake在ubuntu上找到已安装的Boost库

How can I find installed Boost library on ubuntu using CMake?

本文关键字:安装 Boost CMake 何使用 ubuntu      更新时间:2023-10-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可以找到库?

您需要针对boost库进行链接。FindBoost为此提供了变量Boost_LIBRARIES

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

有关更多信息,请参阅FindBoost文档。结尾处有一个例子。

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

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

关于CMake使用boost,您没有遇到任何错误。你只需要告诉它这个系统需要连接进来

要在前面的答案基础上添加内容,以下是需要链接的Boost库列表。(截至Boost 1.65(

只需将收割台包括在内,就可以使用所有其他增压库。