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

时间:2016-08-19 10:02:38

标签: c++ gcc boost cmake

我正在开发一个供一些第三方应用程序使用的库(让我们说是mylibrary)。我的库是由cmake(在CentOS 7和g ++ 4.8上)构建和编译的,并使用一些boost库,例如线程,日志,系统,文件系统和date_time(版本1.61.0)。为了测试我的lib,我开发了一个非常简单的测试器(让我们称之为测试器)。关键是我的库可以通过成功构建和链接,但测试人员不能。让我展示下面的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变量,一切似乎都很好。

1 个答案:

答案 0 :(得分:0)

嗯,这个问题的根本原因与in this question.

完全相同

P.S。 J.J.Hakala在评论中回答了这个问题。我请他把答复写成答案,所以我可以接受。但他还没有回复,我决定自己写答案。如果从S.O.的观点来看这个动作是错误的,或者如果我要求版主让我的Q复制,请告诉我。