gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:20 27 4
gpt4 key购买 nike

我已经使用这个命令安装了 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})

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

关于c++ - 如何使用 CMake 在 ubuntu 上找到已安装的 Boost 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45926763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com