链接boost log教程时链接器错误(未定义的引用)

时间:2014-04-17 15:45:22

标签: c++ boost fedora ld

我已经通过yum在Fedora 20上安装了boost,我正在尝试一些简单的例子。但是我很难从logging tutorial编译第一个例子。

使用g++ -c boosttest.cc进行编译工作正常,但当我尝试将其与

链接时,我遇到了很多错误
g++ boosttest.o -o boosttest -lboost_log -lpthread

完整的错误日志:

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

我错过了什么?我做错了什么?

编辑1: -lboost_log -lpthread的顺序对输出没有影响。还有a very similar question,但解决方案对我不起作用。以下错误也会失败:

g++ -DBOOST_LOG_DYN_LINK boosttest.o -o boosttest -lboost_log -lpthread

13 个答案:

答案 0 :(得分:75)

只需添加一行

即可
#define BOOST_LOG_DYN_LINK 1

作为boosttest.cc的第一行。

或者,您可以在编译步骤中添加-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

答案 1 :(得分:5)

我有同样恼人的问题。 应该在编译时定义宏,而不是链接:

g++ -std=c++11 -DBOOST_LOG_DYN_LINK -c boosttest.cc

在链接器命令中,确保顺序如下:

g++ boosttest.o -lboost_log -lpthread -o boosttest

答案 2 :(得分:3)

-lboost_log_setup为我解决了这个问题。

我是从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

答案 3 :(得分:2)

Boost logger linking issue

中查看Mark Lakata的答案

我的问题,也可能是迈克尔的问题,确实是/ usr / local / lib中存在.dylib和.a。由于我不打算动态链接提升,运行sudo rm -f /usr/local/lib/libboost_*.dylib解决了问题并让我静态链接。我正在处理的程序是用于重新分发,我想分发一个没有依赖项的可执行文件,因此这个页面上的其他解决方案是不可行的。 如果这是不可接受的,您还可以指定lib的完整路径,例如-l / usr / include / lib / libboost_log.a,但这会导致系统相关的makefile。

答案 4 :(得分:2)

您只需添加BOOST_LOG_DYN_LINK定义即可。所以,在像这样的单线命令中:

g++ -DBOOST_LOG_DYN_LINK logging_test.cpp -lboost_log -lpthread

使用该命令,它应该立即运行。

答案 5 :(得分:2)

我正在使用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") ,使其看起来像这样:

-- 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

创建以下输出:

Boost_LIBRARIES

请注意,UNPIVOT由于某种原因一直为空...:o

答案 6 :(得分:1)

有人在addvise中添加cflag BOOST_LOG_DYN_LINK。我认为这不是全部真相。

要使用libboost_log,您应该在构建时知道库的cxxflag。也许标志BOOST_LOG_DYN_LINK没有添加到构建中。

您应该处理该文件 关于BOOST_LOG_VERSION_NAMESPACE的boost / log / detail / config.hpp。

无论如何,你应该使用链接的libboost_log保持相同的boost_log_version_namespace。

要检查boost_log_version_namespace,可以使用以下命令: nm -C libboost_log | grep push_record_move

push_record_move是boost_log的一个函数,它会像这样输出, 升压::登录::的 v2s_mt_posix ::芯:: push_record_move(升压::登录:: v2s_mt_posix ::记录&安培;)

v2s_mt_posix是boost_log_version_namespace。

答案 7 :(得分:1)

如果你想链接boost.log 静态那么你可能需要这样做(按此顺序排列的库)

g++ main.cpp -static -lboost_log -lboost_system -lboost_thread -lpthread -o main

答案 8 :(得分:0)

这只是意味着boost-log库没有正确链接。此外,GCC的订单也很重要。请参阅此question

  1. 首先确保库已正确构建并且包含所有符号。
  2. 尝试更改订单。它可能对你有帮助。

答案 9 :(得分:0)

实际上,只有g++ -DBOOST_ALL_DYN_LINK -lpthread -lboost_log-mt logging_test.cpp适合我。 DBOOST_LOG_DYN_LINK-lboost_log遇到同样的错误。

答案 10 :(得分:0)

如果您想静态链接Boost日志,则需要在Boost编译和项目设置期间使用_WIN32_WINNT和BOOST_LOG_NO_THREADS设置准确,您将使用日志记录。

boost \ 1.67.0 \ include \ boost \ log \ detail \ config.hpp中的以下代码段说明了如何选择日志记录的命名空间。

如果不确定如何编译boost,请尝试在十六进制编辑器中打开库并找到流提供程序。

std @@@ attributes @ v2s_mt_nt6 @ log @ 1 @ _N @ Z。?? 0stream_compound @?$ stream_provider

以下代码段描述了显示链接程序的要求。

// Setup namespace name
#if !defined(BOOST_LOG_DOXYGEN_PASS)
#   if defined(BOOST_LOG_DLL)
#       if defined(BOOST_LOG_NO_THREADS)
#           define BOOST_LOG_VERSION_NAMESPACE v2_st
#       else
#           if defined(BOOST_THREAD_PLATFORM_PTHREAD)
#               define BOOST_LOG_VERSION_NAMESPACE v2_mt_posix
#           elif defined(BOOST_THREAD_PLATFORM_WIN32)
#               if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
#                   define BOOST_LOG_VERSION_NAMESPACE v2_mt_nt6
#               else
#                   define BOOST_LOG_VERSION_NAMESPACE v2_mt_nt5
#               endif
#           else
#               define BOOST_LOG_VERSION_NAMESPACE v2_mt
#           endif
#       endif // defined(BOOST_LOG_NO_THREADS)
#   else
#       if defined(BOOST_LOG_NO_THREADS)
#           define BOOST_LOG_VERSION_NAMESPACE v2s_st
#       else
#           if defined(BOOST_THREAD_PLATFORM_PTHREAD)
#               define BOOST_LOG_VERSION_NAMESPACE v2s_mt_posix
#           elif defined(BOOST_THREAD_PLATFORM_WIN32)
#               if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
#                   define BOOST_LOG_VERSION_NAMESPACE v2s_mt_nt6
#               else
#                   define BOOST_LOG_VERSION_NAMESPACE v2s_mt_nt5
#               endif
#           else
#               define BOOST_LOG_VERSION_NAMESPACE v2s_mt
#           endif
#       endif // defined(BOOST_LOG_NO_THREADS)
#   endif // defined(BOOST_LOG_DLL)

答案 11 :(得分:0)

我也遇到此错误,如果您使用的是CMake,则可以通过以下方式解决此问题,

  1. 查找相关软件包: log log_setup

    find_package(Boost COMPONENTS program_options log log_setup REQUIRED)

2。将log和log_setup附加到其他库中:

  set(PROJECT_LIB ${PROJECT_LIB} ${Boost_LOG_LIBRARY} ${Boost_LOG_SETUP_LIBRARY})
  1. 将这些库链接到您的程序中,

    target_link_libraries(${PROJECT_NAME} -Wl,--start-group ${PROJECT_LIB} -Wl,--end-group)

  2. CMAKE_CXX_FLAGS 标志中添加 DBOOST_LOG_DYN_LINK

    set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC -fopenmp -DBOOST_LOG_DYN_LINK")

答案 12 :(得分:0)

如果只需要静态编译,则可以避免使用标志。在这种情况下,只需在CMake中包含breed [ leadvisitors leadvisitor ] breed [ visitors visitor ] to setup clear-all setup-visitors reset-ticks end to setup-visitors create-leadvisitors num-of-pedestrians * 0.1 ;;create 10% of the total number of pedestrians [ set demand-type "none" set color black set size 1 setxy 0 16 set heading 180 set pen-size 1 set destination one-of patches set wait-time -1 set demand-lvl 0 ] create-visitors (num-of-pedestrians - (num-of-pedestrians * 0.1)) [ set demand-type 0 set size 1 setxy 0 16 set heading 180 set pen-size 1 set destination one-of patches set wait-time -1 set demand-lvl 1 set attracted? false ] ] end to go if turtles = 0 [ stop ] ask turtles [ set-demand-type have-demand ] if ticks > 100 [ stop ] tick display-labels end ;;;;; visitor's internal state of demand ;;;;; to set-demand-type if demand-type = 0 [ set demand-type "food" set color red let target (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = red] ] end to have-demand if demand-lvl = 0 [ ifelse wait-time = -1 [ stroll ] [ set wait-time wait-time - 1 if wait-time = 0 [ stroll ]] ] if demand-lvl = 1 [ ifelse wait-time = -1 [ stroll evaluate ] [ set wait-time wait-time - 1 if wait-time = 0 [ stroll evaluate ]] ] end to stroll if any? neighbors with [ pcolor = gray - 3 ] [ die ] ifelse any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = red] [ facexy exitpt-x exitpt-y ] [ rt random-float visitor-view-angle lt random-float visitor-view-angle ] fd walking-speed end to evaluate if any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = gray - 4 or pcolor = gray - 3 or pcolor = red + 2] [ facexy exitpt-x exitpt-y rt random-float visitor-view-angle lt random-float visitor-view-angle fd walking-speed ] let _mycolor color if any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor] [ let new-target max-one-of ( patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [patch-influence] let dist-to-new-target min-one-of (patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [distance myself] face new-target set heading towards new-target fd walking-speed set attracted? true attracted-and-visiting re-evaluate ] end to attracted-and-visiting if pcolor = red + 2 [ set heading towards one-of patches with [pcolor = red + 2] fd 0 set patch-popularity patch-popularity + 1 set wait-time avg-waiting-time if count turtles-here > 0 [ set num-of-visitors num-of-visitors + 1 ] set plabel num-of-visitors ] end to re-evaluate let _mycolor color if not any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor] [ set attracted? false ] ifelse choose? [set heading towards one-of patches with [pcolor = _mycolor]] [facexy exitpt-x exitpt-y] end to-report choose? report random 2 = 0 end log组件即可。

log_setup