gpt4 book ai didi

c++ - 链接到使用 boost 库的静态库会导致 cmake 中出现链接错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:40 29 4
gpt4 key购买 nike

我正在开发一个库(假设是 mylibrary)以供某些第三方应用程序使用。我的库由 cmake(在 CentOS 7 和 g++ 4.8 上)构建和编译,并使用一些 boost 库,例如线程、日志、系统、文件系统和 date_time(版本 1.61.0)。为了测试我的库,我开发了一个非常简单的测试器(我们称之为测试器)。关键是我的库可以成功构建和链接,但测试人员不能。让我在下面显示 cmake 文件的内容:

在专门用于构建mylibrary的主cmake文件中,我以这种方式添加了boost:

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
SET(Boost_ADDITIONAL_VERSIONS "1.51.0" "1.52.0"
"1.53.0" "1.54.0" "1.55.0" "1.56.0" "1.57.0" "1.58.0"
"1.60.0" "1.61.0" "1.61.0" "1.62.0" "1.63.0" "1.64.0")
find_package(Threads)
find_package(Boost REQUIRED)
find_package(Boost 1.61.0 COMPONENTS thread log log_setup filesystem system date_time)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

为了添加库,我使用了这些代码行:

set(MYLIBRARY_SRC
${MYLIBRARY_SOURCE_DIR}/src/folder1/File1.cpp
${MYLIBRARY_SOURCE_DIR}/src/folder2/File2.cpp
${MYLIBRARY_SOURCE_DIR}/src/folder3/File3.cpp)

add_library (mylibrary ${MYLIBRARY_SRC})

在这个 cmake 文件的最后几行中,我以这种方式添加了 tester 目录:

if(ENABLE_TESTER)
message(STATUS "tester is enabled and will be built")
add_subdirectory (tester)
endif(ENABLE_TESTER)

对于测试人员,我有一个非常简单的 cmake 文件,如下所示:

add_executable(tester tester.cpp)
message(STATUS , "Boost_libraries are ${Boost_LIBRARIES}")
target_link_libraries(tester mylibrary ${Boost_LIBRARIES})

现在,每当我构建我的项目时,我的库都已成功构建,但是当要链接测试器时,我会遇到很多这样的链接器错误:

../../lib/mylibrary.a (Logger.cpp.o): In function `void boost::log::v2s_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::aligned_write<wchar_t>(wchar_t const*, long)':
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:702: undefined reference to `boost::log::v2s_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:696: undefined reference to `boost::log::v2s_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'

重点是我在链接测试器之前打印了 Boost_LIBRARIES 变量,一切看起来都很好。

最佳答案

好吧,这个问题的根本原因与问题完全相同 in this question.

附注J.J.Hakala 在评论中回答了这个问题。我让他写下他的回复作为答案,这样我就可以接受了。但他还没有回复,我决定自己写答案。如果从 S.O. 的角度来看此操作是错误的,或者如果我应该要求版主复制我的 Q,请告诉我。

关于c++ - 链接到使用 boost 库的静态库会导致 cmake 中出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036223/

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