2016-08-19 146 views
1

我正在开发一个库(让我们说mylibrary)以供某些第三方应用程序使用。我的库由cmake(在CentOS 7和g ++ 4.8上)编译和编译,并使用一些boost库,如线程,日志,系统,文件系统和日期时间(版本1.61.0)。为了测试我的库,我开发了一个非常简单的测试工具(我们称之为测试工具)。关键是我的图书馆可以通过成功建立和链接,但测试者不能。让我给下面的cmake文件的内容:以这种方式链接到使用boost库的静态库导致cmake中的链接错误

在主cmake的文件致力于打造在MyLibrary,我已经添加上去:

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的文件的最后一行,我已经加入测试仪目录中这样说:

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,一切似乎罚款。

+0

您可以为库添加cmake代码吗? – wasthishelpful

+0

你检查了这个[答案](http://stackoverflow.com/a/35853967/5781248)? –

+0

@wasthishelpful我添加了代码。 – hsalimi

回答

0

嗯,这个问题的根源是完全一样的问in this question.

附: J.J.哈卡拉在评论中回答了这个问题。我请他把答复写成答案,我可以接受。但他还没有回答,我决定自己写答案。如果这种行为是S.O.的观点是错误的,或者我应该请主持人让我的Q重复,请告诉我。