【问题标题】:Linker error using boost::log in a static lib在静态库中使用 boost::log 的链接器错误
【发布时间】:2016-01-02 18:45:46
【问题描述】:

我有一个使用 Boost.Log 的静态库。当我想在一个单独的测试项目中测试这个库的一些功能时,我得到了以下链接器错误:

undefined reference to `boost::log::v2_mt_posix::sources::aux::global_storage::get_or_init(boost::typeindex::stl_type_index, boost::shared_ptr<boost::log::v2_mt_posix::sources::aux::logger_holder_base> (*)())'

undefined reference to `boost::log::v2_mt_posix::sources::aux::throw_odr_violation(boost::typeindex::stl_type_index, boost::typeindex::stl_type_index, boost::log::v2_mt_posix::sources::aux::logger_holder_base const&)'

undefined reference to `boost::log::v2_mt_posix::aux::code_convert_impl(wchar_t const*, unsigned long, std::string&, std::locale const&)'

我在配置 Boost.Log 并安装全局记录器的静态库中有一个记录器类。我真的不知道我必须与测试项目链接什么才能成功链接。也许你有想法?

使用 CMake,我将以下库添加到静态库中:

target_link_libraries(RBSLib
        boost_system
        boost_filesystem
        boost_python
        boost_date_time
        boost_program_options
        boost_log
        boost_thread        
        pq
        ssl
        crypto
        boost_regex
        pthread
        dl
)

以及我的测试项目的以下库:

target_link_libraries(${PROJECT_NAME}
        boost_system
        boost_date_time
        boost_unit_test_framework
        tbb
        pq
        ssl
        pthread
${PROJECT_BINARY_DIR}/../rbs/libRBSLib.a
        boost_thread
        boost_filesystem
        boost_program_options
        crypto
        boost_log
)

【问题讨论】:

    标签: c++ logging boost linker


    【解决方案1】:

    您需要添加新的库依赖项以及在编译静态库时所做的相同操作。换句话说,您需要将 boost 的包含路径和库(编译您的库所需要的)添加到测试项目的编译器和链接器属性中。这是因为您的新库没有与 boost(或任何其他库)静态链接在一起。它仍然需要明确的外部库。

    【讨论】:

    • 我想我已经按照你的解释做了。我已经用一些 CMake 部件更新了我的帖子,也许库的顺序有错误?当我向下移动 libRBSLib.a 条目时,错误发生了变化。
    • @Paul 我不认为这是库的顺序。您的测试项目的依赖项应该涉及静态库的依赖项。要么删除静态库配置中不必要的库,要么将它们添加到测试项目中(例如 boost_python)。
    • 好的,我还没有将 boost lib 目录添加到我的测试项目的 Cmake 文件中。现在它编译了:-)
    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多