该库未通过 CmakeLists.txt 链接

问题描述 投票:0回答:1

我安装了boost库。之后我还通过 ./bootstrap 和 ./b2 构建库。 但由于某种原因,在 QT Creator 中构建时出现以下错误:

LNK2019: unresolved external symbol "bool __cdecl boost::this_thread::interruptible_wait(void *,struct boost::detail::mono_platform_timepoint const &)" (?interruptible_wait@this_thread@boost@@YA_NPEAXAEBUmono_platform_timepoint@detail@2@@Z) referenced in function "void __cdecl boost::this_thread::sleep_for<__int64,struct std::ratio<1,1> >(class boost::chrono::duration<__int64,struct std::ratio<1,1> > const &)" (??$sleep_for@_JU?$ratio@$00$00@std@@@this_thread@boost@@YAXAEBV?$duration@_JU?$ratio@$00$00@std@@@chrono@1@@Z)
testin.exe:-1: error: LNK1120: 1 unresolved externals
:-1: error: ninja: build stopped: subcommand failed.

这是我的简单代码示例:

#include <iostream>
#include "boost/chrono.hpp"
#include "boost/thread.hpp"
int main()
{
    boost::this_thread::sleep_for(boost::chrono::seconds(1));
    return 0;
}

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)

project(testin LANGUAGES CXX)

add_executable(testin main.cpp)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

set(BOOST_ROOT "C:/libraries/boost_1_85_0")
set(BOOST_INCLUDEDIR "C:/libraries/boost_1_85_0")
set(BOOST_LIBRARYDIR "C:/libraries/boost_1_85_0/lib64-msvc-14.3")

add_definitions(-DBOOST_ALL_NO_LIB)

find_package(Boost 1.85.0 REQUIRED)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

include_directories(${Boost_INCLUDE_DIRS})
link_directories(${BOOST_LIBRARYDIR})

target_link_libraries(testin Qt${QT_VERSION_MAJOR}::Core)
target_link_libraries(testin ${Boost_LIBRARIES})

include(GNUInstallDirs)
install(TARGETS testin
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

我怀疑问题出在我的 CMakLists.txt 中。因为类似的代码在 Visual Studio 2022 中运行没有问题。但是在 Qt Creator 中使用 CMakeLists.txt 时出现了问题。

c++ qt cmake linker
1个回答
0
投票

出于某种我不明白的原因,我应该这样写:

find_package(Boost 1.85.0 COMPONENTS thread system chrono REQUIRED)

而不是这个:

find_package(Boost 1.85.0 REQUIRED)

此外,由于某种原因

link_libraries()
不起作用。而且有必要使用
target_link_libraries()

这是为什么?

© www.soinside.com 2019 - 2024. All rights reserved.