C++ 链接到使用boost库的静态库会导致cmake中的链接错误

C++ 链接到使用boost库的静态库会导致cmake中的链接错误,c++,gcc,boost,cmake,C++,Gcc,Boost,Cmake,我正在开发一个库(比方说mylibrary),供一些第三方应用程序使用。我的库由cmake(在CentOS 7和g++4.8上)构建和编译,并使用一些boost库,如线程、日志、系统、文件系统和日期时间(版本1.61.0)。为了测试我的库,我开发了一个非常简单的测试仪(我们称之为tester)。关键是我的库可以成功地构建和链接,但是测试人员不能。让我在下面展示cmake文件的内容: 在专用于构建mylibrary的主cmake文件中,我以这种方式添加了boost: set(Boost_USE_S

我正在开发一个库(比方说mylibrary),供一些第三方应用程序使用。我的库由cmake(在CentOS 7和g++4.8上)构建和编译,并使用一些boost库,如线程、日志、系统、文件系统和日期时间(版本1.61.0)。为了测试我的库,我开发了一个非常简单的测试仪(我们称之为tester)。关键是我的库可以成功地构建和链接,但是测试人员不能。让我在下面展示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})
if(ENABLE_TESTER)
    message(STATUS "tester is enabled and will be built")
    add_subdirectory (tester)
endif(ENABLE_TESTER)
为了添加库,我使用了以下代码行:

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目录:

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})
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&)'
现在,每当我构建我的项目时,我的库都会成功构建,但是当测试人员要被链接时,我会面临很多链接器错误,比如:

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&)'
。/../lib/mylibrary.a(Logger.cpp.o):在函数“void boost::log::v2s\u mt\u posix::basic\u formatting\u ostream::aligned\u write(wchar\u t const*,long)”中:
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:702:boost::log::v2s\u mt\u posix::aux::code\u const*,unsigned long,std::string&,std::locale const&)的未定义引用
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:696:boost::log::v2s\u mt\u posix::aux::code\u const*,unsigned long,std::string&,std::locale const&)的未定义引用

关键是,在链接测试仪之前,我已经打印了Boost_LIBRARIES变量,一切看起来都很好

那么,这个问题的根本原因也完全一样


p、 s.J.J.Hakala在评论中回答了这个问题。我请他写回信作为答复,这样我就可以接受了。但他还没有回答,我决定自己写答案。如果这个行为是错误的形式S.O.的观点,或者如果我应该要求主持人使我的Q重复,请让我知道

那么,这个问题的根本原因也完全一样


p、 s.J.J.Hakala在评论中回答了这个问题。我请他写回信作为答复,这样我就可以接受了。但他还没有回答,我决定自己写答案。如果这个行为是错误的形式S.O.的观点,或者如果我应该要求主持人使我的Q重复,请让我知道

你能为我的图书馆添加cmake代码吗?你检查过了吗?@wasthis有帮助吗?我添加了代码。@J.J.Hakala是的,重点是我只有一个版本的boost,那就是1.61.0。@J.J.Hakala你在哪里对J.J.Hakala。不幸的是,我使用了一个旧的linux映像,上面安装了一些旧的boost。所以,请添加您的评论作为答案,这样我就可以接受了。您能为我的图书馆添加cmake代码吗?您检查过了吗?@wasthis有帮助吗?我添加了代码。@J.J.Hakala是的,重点是我只有一个版本的boost,那就是1.61.0。@J.J.Hakala您在哪里,对J.J.Hakala。不幸的是,我使用了一个旧的linux映像,上面安装了一些旧的boost。所以,请添加您的评论作为回答,这样我就可以接受了。