当包含init_from_settings.hpp时,使用静态链接编译boost :: log不起作用

时间:2018-11-30 05:31:33

标签: c++ boost

我能够成功编译以下程序。

#include <iostream>

#include "boost/log/trivial.hpp"

int main(int, char**)
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    return EXIT_SUCCESS;
}

使用以下CMakeFile。

cmake_minimum_required(VERSION 2.8)

project(boost_log CXX)

# we will use static libs
set(Boost_USE_STATIC_LIBS ON)

# Boost::log required Boost version >= 1.54.0 
find_package(Boost 1.54.0 REQUIRED COMPONENTS log)

find_package(Threads)

include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

set(SRC_FILES
    src/main.cpp)

add_executable(boost_log ${SRC_FILES})
target_link_libraries(boost_log ${Boost_LIBRARIES} Threads::Threads)

但是,如果我编辑main.cpp并添加以下代码段:

#include "boost/log/utility/setup.hpp"
#include "boost/log/utility/setup/from_settings.hpp"

void blah(void)
{

    boost::log::settings setts;
    boost::log::init_from_settings(setts);
}

然后出现链接错误:

/usr/bin/c++    -rdynamic CMakeFiles/boost_log.dir/src/main.cpp.o  -o boost_log -Wl,-Bstatic -lboost_log -lboost_date_time -lboost_log_setup -lboost_system -lboost_filesystem -lboost_thread -lboost_regex -lboost_chrono -lboost_atomic -Wl,-Bdynamic -lpthread -lpthread 
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_log_setup.a(init_from_settings.o): In function `boost::log::v2s_mt_posix::(anonymous namespace)::default_syslog_sink_factory<char>::create_sink(boost::log::v2s_mt_posix::basic_settings_section<char> const&)':
(.text+0x27fa): undefined reference to `boost::log::v2s_mt_posix::sinks::syslog_backend::set_severity_mapper(boost::log::v2s_mt_posix::aux::light_function<boost::log::v2s_mt_posix::sinks::syslog::level (boost::log::v2s_mt_posix::record_view const&)> const&)'
(snipped, but the errors continue)

为什么包含from_settings.hpp会导致链接失败?这不是静态链接吗?

我已经在Fedora和Ubuntu上尝试了相同的结果。

1 个答案:

答案 0 :(得分:0)

您缺少log_setup库!

要做:

find_package(Boost 1.54.0 REQUIRED COMPONENTS log_setup log)