【问题标题】:Linking to a static library that uses boost libraries causes link errors in cmake链接到使用 boost 库的静态库会导致 cmake 中的链接错误
【发布时间】:2016-08-19 10:02:37
【问题描述】:

我正在开发一个供某些第三方应用程序使用的库(比如 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 变量,一切看起来都很好。

【问题讨论】:

  • 可以添加mylibrary的cmake代码吗?
  • 你检查过这个answer 吗?
  • @wasthishelpful 我添加了代码。
  • @J.J.Hakala 是的,关键是我只有一个版本的 boost,即 1.61.0。
  • @J.J.Hakala 你在哪里对 J.J.哈卡拉。不幸的是,我使用了一个用过的 linux 镜像,上面安装了一些旧的 bo​​ost。因此,请添加您的评论作为答案,以便我接受。

标签: c++ gcc boost cmake


【解决方案1】:

嗯,这个问题的根本原因和in this question.问的完全一样

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多