簡體   English   中英

鏈接到使用boost庫的靜態庫會導致cmake中的鏈接錯誤

[英]Linking to a static library that uses boost libraries causes link errors in 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變量,一切似乎都很好。

那么,這個問題的根本原因與這個問題完全相同

ps JJHakala在評論中回答了這個問題。 我請他把答復寫成答案,所以我可以接受。 但他還沒有回復,我決定自己寫答案。 如果從SO的角度來看這個動作是錯誤的,或者如果我要求版主讓我的Q復制,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM