Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

c++ - Boost not Linking properly using CMake on Ubuntu

I've tried to make the code as simple as possible and I'm still getting

undefined reference to `boost::log::v2_mt_posix:: . . . etc.

This is driving me crazy, so I'm posting my code:

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(BoostTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -DBOOST_LOG_DYN_LINK")

set(SOURCE_FILES
    Logger.cpp
    Logger.h
    loggertest.cpp)

set(BOOST_ROOT /usr/local)

find_package(Boost 1.57.0 COMPONENTS log thread system REQUIRED)

include_directories(${Boost_INCLUDE_DIRS})

add_executable(BoostTest ${SOURCE_FILES})

target_link_libraries(BoostTest ${Boost_LIBRARIES})

loggertest.cpp

#include "Logger.h"

using namespace idair;

int main(int argc, char* argv[]) {
    Logger log;
    log.trivialLogging();
    return 0;
}

Logger.h

#ifndef ONEPRINT_LOGGER_H
#define ONEPRINT_LOGGER_H

#include <boost/log/core/core.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/core/null_deleter.hpp>

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sources = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

namespace idair {

    enum severity_level
    {
        normal,
        warning,
        error,
        critical
    };

    class Logger {
    public:
        void trivialLogging();

    };

}
#endif //ONEPRINT_LOGGER_H

Logger.cpp

#include "Logger.h"

using namespace idair;

void Logger::trivialLogging() {
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}

ERRORS

Scanning dependencies of target BoostTest
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/Logger.cpp.o
[100%] Building CXX object CMakeFiles/BoostTest.dir/loggertest.cpp.o
Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `idair::Logger::trivialLogging()':
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:16: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:17: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: undefined reference to `boost::log::v2_mt_posix::trivial::logger::get()'
CMakeFiles/BoostTest.dir/Logger.cpp.o:/home/pdl/ClionProjects/BoostTest/Logger.cpp:18: more undefined references to `boost::log::v2_mt_posix::trivial::logger::get()' follow
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record::reset()':
/usr/local/include/boost/log/core/record.hpp:153: undefined reference to `boost::log::v2_mt_posix::record_view::public_data::destroy(boost::log::v2_mt_posix::record_view::public_data const*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_composite_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex>, boost::log::v2_mt_posix::sources::features<boost::log::v2_mt_posix::sources::severity<boost::log::v2_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:456: undefined reference to `boost::log::v2_mt_posix::core::get_logging_enabled() const'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::~record_pump()':
/usr/local/include/boost/log/sources/record_ostream.hpp:278: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>&, boost::log::v2_mt_posix::record&)':
/usr/local/include/boost/log/sources/record_ostream.hpp:258: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2_mt_posix::record&)'
/usr/local/include/boost/log/sources/record_ostream.hpp:259: undefined reference to `boost::log::v2_mt_posix::aux::unhandled_exception_count()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level> >::auto_release::~auto_release()':
/usr/local/include/boost/log/sources/record_ostream.hpp:243: undefined reference to `boost::log::v2_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2_mt_posix::aux::stream_provider<char>::stream_compound*)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::sources::aux::severity_level<boost::log::v2_mt_posix::trivial::severity_level>::set_value(boost::log::v2_mt_posix::trivial::severity_level)':
/usr/local/include/boost/log/sources/severity_feature.hpp:135: undefined reference to `boost::log::v2_mt_posix::sources::aux::get_severity_level()'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::record boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::severity_logger_mt<boost::log::v2_mt_posix::trivial::severity_level>, boost::log::v2_mt_posix::sources::multi_thread_model<boost::log::v2_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::severity, boost::log::v2_mt_posix::trivial::severity_level const> const&)':
/usr/local/include/boost/log/sources/basic_logger.hpp:259: undefined reference to `boost::log::v2_mt_posix::core::open_record(boost::log::v2_mt_posix::attribute_set const&)'
CMakeFiles/BoostTest.dir/Logger.cpp.o: In function `boost::log::v2_mt_posix::core::push_record(boost::log::v2_mt_posix::record&&)':
/usr/local/include/boost/log/core/core.hpp:308: undefined reference to `boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&)'
collect2: error: ld returned 1 exit status
make[3]: *** [BoostTest] Error 1
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
make: *** [BoostTest] Error 2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you installed a static version of boost.

The boost::log::v2_mt_posix namespace is implemented in dynamically built versions of boost (see this link). Your application will attempt to link against symbols from this namespace if you define the BOOST_LOG_DYN_LINK macro during compilation.

If you have the statically built version of Boost.Log installed to /usr/local, try removing the -DBOOST_LOG_DYN_LINK from your ${CMAKE_CXX_FLAGS} line, regenerate your build directory and attempt the build again. (Side note: typically you will add compiler definitions with the add_definitions() command, see this link)

Or, reinstall a dynamic version of boost.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...