【问题标题】:Cannot link Boost library to C++14 app using CMake无法使用 CMake 将 Boost 库链接到 C++14 应用程序
【发布时间】:2019-02-07 11:48:28
【问题描述】:

我尝试搜索相同的问题,但没有一个解决方案对我不起作用。我无法编译它。附上 cmake 文件和错误代码。

Cmake 文件:

cmake_minimum_required(VERSION 3.13.3)
project(proj)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(Threads)
find_package(Boost 1.68.0 REQUIRED COMPONENTS system filesystem thread)

include_directories(include ${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

file(GLOB SOURCES source/*.cpp)

message(${Boost_LIBRARIES})
message(${Boost_LIBRARY_DIRS})
message(${Boost_INCLUDE_DIRS})

add_executable(proj ${SOURCES})
target_link_libraries(proj ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})

链接器错误:

Main.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x5): undefined reference to `boost::system::detail::system_category_instance'
CMakeFiles/proj.dir/source/Main.cpp.o: In function `boost::system::generic_category()':
Main.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x5): undefined reference to `boost::system::detail::generic_category_instance'
CMakeFiles/proj.dir/source/Server.cpp.o: In function `boost::asio::detail::socket_ops::close(int, unsigned char&, bool, boost::system::error_code&)':
Server.cpp:(.text._ZN5boost4asio6detail10socket_ops5closeEiRhbRNS_6system10error_codeE[_ZN5boost4asio6detail10socket_ops5closeEiRhbRNS_6system10error_codeE]+0x6b): undefined reference to `boost::system::detail::system_category_instance'
CMakeFiles/proj.dir/source/Server.cpp.o: In function `boost::asio::detail::socket_holder::~socket_holder()':

方法:

void Server::startListening()
{
    while (true)
    {
        tcp::socket socket(m_io_service);

        m_acceptor.accept(socket);
        std::thread t(&Server::handleConnection, this, std::move(socket));
        t.detach();
    }
}

void Server::handleConnection(tcp::socket socket)
{
    ...
}

【问题讨论】:

  • find_package前加ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)是否有效?
  • @Superlokkus 该宏用于 Boost 日志,OP 似乎没有使用它。
  • @Someprogrammerdude 第一:我们不确定,因为 OP 确实发布了代码,链接问题可能会误导第二:有更好的想法吗?因为我无法复制它
  • @Superlokkus 不,它不能解决我的问题。使用 boost::asio 添加了两个方法。
  • @bpieszko 除非您使用非常旧的 CMake 版本,否则 Boost package documentation 会告诉您改用“目标”语法,如 target_link_libraries(proj ${CMAKE_THREAD_LIBS_INIT} Boost::thread Boost::filesystem Boost::system) 中的那样它会改变什么吗? (我对此表示怀疑,但至少尝试一下)。

标签: c++ boost cmake boost-asio


【解决方案1】:

你不应该使用基于目录的api,而应该使用基于目标的api。

正如@Someprogrammerdude 在 cmets 中所说,您应该像这样找到并使用 boost 库:

find_package(Boost 1.68.0 REQUIRED COMPONENTS system filesystem thread)

file(GLOB SOURCES source/*.cpp) # File GLOB is a bad CMake practice
add_executable(proj ${SOURCES})

target_link_libraries(proj PUBLIC
    boost::system
    boost::filesystem
    boost::thread
)

target_link_libraries 将添加所有必要的包含目录和链接器标志。

【讨论】:

  • 这对我没有帮助。我测试了所有的想法。问题是我有 C++11 Boost 版本。降级 C++ 版本对我有帮助。
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多