C++ 链接 boost 日志教程时的链接器错误(未定义的参考)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23137637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linker error while linking boost log tutorial (undefined references)
提问by Michael Gro?e
I have installed boost on Fedora 20 via yum and am trying some simple examples. However I have trouble to compile the first example from the logging tutorial.
我已经通过 yum 在 Fedora 20 上安装了 boost 并且正在尝试一些简单的例子。但是,我无法编译日志教程中的第一个示例。
Compiling with g++ -c boosttest.cc
works fine, but I get lot's of errors when I try to link it with
编译g++ -c boosttest.cc
工作正常,但是当我尝试将它与
g++ boosttest.o -o boosttest -lboost_log -lpthread
the full error-log:
完整的错误日志:
boosttest.o: In function `main':
boosttest.cc:(.text+0x44): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
boosttest.cc:(.text+0x9a): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
boosttest.cc:(.text+0x167): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
boosttest.cc:(.text+0x1bd): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
boosttest.cc:(.text+0x28a): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
boosttest.o:boosttest.cc:(.text+0x2e0): more undefined references to `boost::log::v2s_mt_posix::trivial::logger::get()' follow
boosttest.o: In function `boost::log::v2s_mt_posix::record::reset()':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix6record5resetEv[_ZN5boost3log12v2s_mt_posix6record5resetEv]+0x18): undefined reference to `boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)'
boosttest.o: In function `boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_composite_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex>, boost::log::v2s_mt_posix::sources::features<boost::log::v2s_mt_posix::sources::severity<boost::log::v2s_mt_posix::trivial::severity_level>, void, void, void, void, void, void, void, void, void> >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&)':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix7sources22basic_composite_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEENS2_8featuresINS2_8severityIS6_EEvvvvvvvvvEEE11open_recordINS_9parameter3aux15tagged_argumentINS1_8keywords3tag8severityEKS6_EEEENS1_6recordERKT_[_ZN5boost3log12v2s_mt_posix7sources22basic_composite_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEENS2_8featuresINS2_8severityIS6_EEvvvvvvvvvEEE11open_recordINS_9parameter3aux15tagged_argumentINS1_8keywords3tag8severityEKS6_EEEENS1_6recordERKT_]+0x1e): undefined reference to `boost::log::v2s_mt_posix::core::get_logging_enabled() const'
boosttest.o: In function `boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::~record_pump()':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEED2Ev[_ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEED5Ev]+0x2c): undefined reference to `boost::log::v2s_mt_posix::aux::unhandled_exception_count()'
boosttest.o: In function `boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>&, boost::log::v2s_mt_posix::record&)':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEEC2ERS8_RNS1_6recordE[_ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEEC5ERS8_RNS1_6recordE]+0x1d): undefined reference to `boost::log::v2s_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2s_mt_posix::record&)'
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEEC2ERS8_RNS1_6recordE[_ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEEC5ERS8_RNS1_6recordE]+0x28): undefined reference to `boost::log::v2s_mt_posix::aux::unhandled_exception_count()'
boosttest.o: In function `boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::auto_release::~auto_release()':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEE12auto_releaseD2Ev[_ZN5boost3log12v2s_mt_posix3aux11record_pumpINS1_7sources18severity_logger_mtINS1_7trivial14severity_levelEEEE12auto_releaseD5Ev]+0xf): undefined reference to `boost::log::v2s_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2s_mt_posix::aux::stream_provider<char>::stream_compound*)'
boosttest.o: In function `boost::log::v2s_mt_posix::sources::aux::severity_level<boost::log::v2s_mt_posix::trivial::severity_level>::set_value(boost::log::v2s_mt_posix::trivial::severity_level)':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix7sources3aux14severity_levelINS1_7trivial14severity_levelEE9set_valueES6_[_ZN5boost3log12v2s_mt_posix7sources3aux14severity_levelINS1_7trivial14severity_levelEE9set_valueES6_]+0x7): undefined reference to `boost::log::v2s_mt_posix::sources::aux::get_severity_level()'
boosttest.o: In function `boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&)':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix7sources12basic_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEEE20open_record_unlockedINS_9parameter3aux15tagged_argumentINS1_8keywords3tag8severityEKS6_EEEENS1_6recordERKT_[_ZN5boost3log12v2s_mt_posix7sources12basic_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEEE20open_record_unlockedINS_9parameter3aux15tagged_argumentINS1_8keywords3tag8severityEKS6_EEEENS1_6recordERKT_]+0x2a): undefined reference to `boost::log::v2s_mt_posix::core::open_record(boost::log::v2s_mt_posix::attribute_set const&)'
boosttest.o: In function `boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::push_record_unlocked(boost::rv<boost::log::v2s_mt_posix::record>&)':
boosttest.cc:(.text._ZN5boost3log12v2s_mt_posix7sources12basic_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEEE20push_record_unlockedERNS_2rvINS1_6recordEEE[_ZN5boost3log12v2s_mt_posix7sources12basic_loggerIcNS2_18severity_logger_mtINS1_7trivial14severity_levelEEENS2_18multi_thread_modelINS1_3aux14light_rw_mutexEEEE20push_record_unlockedERNS_2rvINS1_6recordEEE]+0x36): undefined reference to `boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)'
collect2: error: ld returned 1 exit status
What am I missing? What am I doing wrong?
我错过了什么?我究竟做错了什么?
EDIT 1:The order of -lboost_log -lpthread
makes no difference in the output. There is also a very similar question, but the solution does not work for me. The following also fails with the same errors:
编辑 1:的顺序-lboost_log -lpthread
在输出中没有区别。还有一个非常相似的问题,但该解决方案对我不起作用。以下也失败并出现相同的错误:
g++ -DBOOST_LOG_DYN_LINK boosttest.o -o boosttest -lboost_log -lpthread
回答by Adrian Heine
Just add a line
只需添加一行
#define BOOST_LOG_DYN_LINK 1
as the first line of boosttest.cc
.
作为 的第一行boosttest.cc
。
Alternatively, you can add -DBOOST_LOG_DYN_LINK
to you compilation step (not the linking step, as you posted in the question):
或者,您可以添加-DBOOST_LOG_DYN_LINK
到编译步骤(而不是您在问题中发布的链接步骤):
g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK -c boosttest.cc
g++ boosttest.o -lpthread -lboost_log -o boosttest
回答by user4062498
I had the same annoying problem. The macro should be defined while compiling, not linking:
我有同样烦人的问题。宏应该在编译时定义,而不是链接:
g++ -std=c++11 -DBOOST_LOG_DYN_LINK -c boosttest.cc
In the linker command, make sure the order is as follows:
在链接器命令中,确保顺序如下:
g++ boosttest.o -lboost_log -lpthread -o boosttest
回答by palik
-lboost_log_setup
solved the issue for me.
-lboost_log_setup
为我解决了这个问题。
I borrowed it from spdlog bench Makefile
g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK -c boost-bench-mt.cpp
g++ boost-bench-mt.o -lpthread -lboost_log -lboost_log_setup -lboost_system -lboost_thread -o boost-bench-mt
回答by GPrathap
I also had this error, if you are using CMake you may able to resolve this issue in the following way,
我也有这个错误,如果你使用 CMake 你可以通过以下方式解决这个问题,
Finding related packages: log log_setup
find_package(Boost COMPONENTS program_options log log_setup REQUIRED)
查找相关包:log log_setup
find_package(Boost COMPONENTS program_options log log_setup REQUIRED)
2.Append log and log_setup into other libs:
2.将 log 和 log_setup 附加到其他库中:
set(PROJECT_LIB ${PROJECT_LIB} ${Boost_LOG_LIBRARY} ${Boost_LOG_SETUP_LIBRARY})
Link those libraries into your program,
target_link_libraries(${PROJECT_NAME} -Wl,--start-group ${PROJECT_LIB} -Wl,--end-group)
Add DBOOST_LOG_DYN_LINKinto CMAKE_CXX_FLAGSflags
set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC -fopenmp -DBOOST_LOG_DYN_LINK")
将这些库链接到您的程序中,
target_link_libraries(${PROJECT_NAME} -Wl,--start-group ${PROJECT_LIB} -Wl,--end-group)
将DBOOST_LOG_DYN_LINK添加到CMAKE_CXX_FLAGS标志中
set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC -fopenmp -DBOOST_LOG_DYN_LINK")
回答by stdcerr
I'm using cmake
and get the same error on make
, I inserted set (CMAKE_CXX_FLAGS "-g -Wall -DBOOST_LOG_DYN_LINK")
into my CMakeLists.txt
to make it look like:
我正在使用cmake
并在 上遇到相同的错误make
,我将其插入set (CMAKE_CXX_FLAGS "-g -Wall -DBOOST_LOG_DYN_LINK")
到我的中CMakeLists.txt
以使其看起来像:
cmake_minimum_required(VERSION 2.6)
project(LOGGER)
set(BOOST_INCLUDEDIR "/path/to/env/include")
set(BOOST_ROOT "/path/to/env/include")
find_package(Boost REQUIRED)
message(STATUS Boost_LIBRARIES:)
message (STATUS ${Boost_LIBRARIES})
message(STATUS BOOST_INCLUDEDIR:)
message(STATUS ${BOOST_INCLUDEDIR})
ADD_EXECUTABLE(logger logger.cpp)
target_include_directories(logger PUBLIC ${BOOST_INCLUDEDIR})
set (CMAKE_CXX_FLAGS "-g -Wall -DBOOST_LOG_DYN_LINK")
which creates this output:
创建此输出:
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.67.0
-- Boost_LIBRARIES:
--
-- BOOST_INCLUDEDIR:
-- /path/to/env/include
-- Configuring done
-- Generating done
Mind that Boost_LIBRARIES
stays empty for some reason... :o
Boost_LIBRARIES
由于某种原因而保持空虚的头脑......:o
回答by Tony BenBrahim
Look at Mark Lakata's answer in Boost logger linking issue
在Boost logger linking issue 中查看 Mark Lakata 的回答
My problem, and probably Michael's problem, is indeed the presence of both .dylib and .a in /usr/local/lib. Since I do not plan to ever link boost dynamically, running sudo rm -f /usr/local/lib/libboost_*.dylib
solved the problem and let me link statically. The program I am working on is for redistribution, and I want to distribute one executable without dependencies, so other solutions on this page were not workable.
If that is not acceptable, you could also specify the full path to the lib, like -l/usr/include/lib/libboost_log.a, but that results in system dependent makefiles.
我的问题,也可能是 Michael 的问题,确实是在 /usr/local/lib 中同时存在 .dylib 和 .a。由于我不打算动态链接提升,运行sudo rm -f /usr/local/lib/libboost_*.dylib
解决了问题并让我静态链接。我正在开发的程序用于重新分发,我想分发一个没有依赖关系的可执行文件,因此此页面上的其他解决方案不可行。如果这是不可接受的,您还可以指定 lib 的完整路径,如 -l/usr/include/lib/libboost_log.a,但这会导致依赖于系统的 makefile。
回答by silgon
You just need to add the BOOST_LOG_DYN_LINK
definition. So, in one-liner command like so:
您只需要添加BOOST_LOG_DYN_LINK
定义。因此,在像这样的单行命令中:
g++ -DBOOST_LOG_DYN_LINK logging_test.cpp -lboost_log -lpthread
With that command it should be running now.
使用该命令,它现在应该正在运行。
回答by Loach Fish
Someone addvise to add the cflag BOOST_LOG_DYN_LINK. I think it is not all the truth.
有人建议添加 cflag BOOST_LOG_DYN_LINK。我认为这不是全部的真相。
To use the libboost_log, you should to know the cxxflag of the library when it build. Maybe the flag BOOST_LOG_DYN_LINK is not added into the build.
要使用 libboost_log,您应该在构建时知道库的 cxxflag。也许标志 BOOST_LOG_DYN_LINK 没有添加到构建中。
You should take care of the file boost/log/detail/config.hpp about the BOOST_LOG_VERSION_NAMESPACE.
您应该注意有关 BOOST_LOG_VERSION_NAMESPACE 的文件 boost/log/detail/config.hpp。
Anyway you should keep the same boost_log_version_namespace with the libboost_log that linked.
无论如何,您应该与链接的 libboost_log 保持相同的 boost_log_version_namespace。
To check the boost_log_version_namespace can use the command: nm -C libboost_log | grep push_record_move
要检查 boost_log_version_namespace 可以使用命令: nm -C libboost_log | grep push_record_move
push_record_move is one function of boost_log, it will output,like this, boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)
push_record_move 是 boost_log 的一个函数,它会像这样输出 boost::log:: v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)
v2s_mt_posix is the boost_log_version_namespace.
v2s_mt_posix 是 boost_log_version_namespace。
回答by speedcell4
actually, only g++ -DBOOST_ALL_DYN_LINK -lpthread -lboost_log-mt logging_test.cpp
works for me. DBOOST_LOG_DYN_LINK
or -lboost_log
got the same error.
实际上,只g++ -DBOOST_ALL_DYN_LINK -lpthread -lboost_log-mt logging_test.cpp
对我有用。DBOOST_LOG_DYN_LINK
或-lboost_log
得到同样的错误。
回答by lazzlo
If you want link boost.log staticallythen maybe you need do this (libraries in this order)
如果您想静态链接 boost.log那么您可能需要这样做(按此顺序排列库)
g++ main.cpp -static -lboost_log -lboost_system -lboost_thread -lpthread -o main