我试图让代码变得尽可能简单,但我仍然没有
undefined reference to `boost::log::v2_mt_posix:: . . . etc.
这让我发疯,所以我发布了我的代码:
的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";
}
错误
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
答案 0 :(得分:1)
看起来你安装了一个静态版本的boost。
boost::log::v2_mt_posix
命名空间在动态构建的boost版本中实现(请参阅this link)。如果在编译期间定义BOOST_LOG_DYN_LINK宏,您的应用程序将尝试链接此命名空间中的符号。
如果您已将静态构建的Boost.Log版本安装到/ usr / local,请尝试从-DBOOST_LOG_DYN_LINK
行中删除${CMAKE_CXX_FLAGS}
,重新生成构建目录并再次尝试构建。 (旁注:通常您将使用add_definitions()
命令添加编译器定义,请参阅this link)
或者,重新安装动态版本的boost。