异常的C++链接错误 - 'Defined in discarded section'

22

我试图链接我的程序可执行文件,但一直出现错误。

具体表现为

boost::this_thread::sleep(boost::posix_time::milliseconds(10));

我的源代码在链接器中产生以下错误:

`.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<std::runtime_error>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED2Ev[_ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_year> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_year> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_year>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_month>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)

使用 boost::thread::condition_variable::wait 会产生如下效果

src/net/libnet.a(semaphore.cpp.o): In function `interruption_checker':
/usr/local/include/boost/thread/pthread/thread_data.hpp:96: undefined reference to `boost::detail::get_current_thread_data()'
src/net/libnet.a(semaphore.cpp.o): In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:57: undefined reference to `boost::this_thread::interruption_point()'
collect2: ld returned 1 exit status

我正在使用CMake,它告诉我Make会执行以下命令,但是会失败:

/usr/bin/c++       CMakeFiles/rcon.dir/src/main.cpp.o  -o rcon -rdynamic -L/usr/local/lib /usr/local/lib/libboost_system.a /usr/local/lib/libboost_thread.a -lpthread src/net/libnet.a src/log/liblogging.a -Wl,-rpath,/usr/local/lib 

问题可能出在哪里?它与libboost_thread.a的链接有关吗?

更新

将我的程序中编译成静态库的libnet和liblogging(因为我认为这很整洁)放在命令行中boost库之前可以消除undefined错误。然而,现在当我在代码中使用boost::condition_variable编译时,会出现以下错误,而另一个错误则没有改变:

`.text._ZN5boost16exception_detail19error_info_injectorINS_10lock_errorEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_10lock_errorEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::lock_error>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_10lock_errorEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_10lock_errorEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<std::runtime_error>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED2Ev[_ZN5boost16exception_detail19error_info_injectorISt13runtime_errorED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian16bad_day_of_monthEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_21thread_resource_errorEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_21thread_resource_errorEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::thread_resource_error>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_21thread_resource_errorEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_21thread_resource_errorEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::condition_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::condition_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_15condition_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_15condition_errorEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_15condition_errorEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::condition_error>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_15condition_errorEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_15condition_errorEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_year> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_year> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian8bad_yearEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_year>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian8bad_yearEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED2Ev' referenced in section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED1Ev[non-virtual thunk to boost::exception_detail::error_info_injector<boost::gregorian::bad_month>::~error_info_injector()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED2Ev[_ZN5boost16exception_detail19error_info_injectorINS_9gregorian9bad_monthEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian16bad_day_of_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorISt13runtime_errorEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_month> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_9gregorian9bad_monthEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_21thread_resource_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)
`.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED2Ev' referenced in section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED1Ev[non-virtual thunk to boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >::~clone_impl()]' of /usr/local/lib/libboost_thread.a(thread.o): defined in discarded section `.text._ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED2Ev[_ZN5boost16exception_detail10clone_implINS0_19error_info_injectorINS_10lock_errorEEEED5Ev]' of /usr/local/lib/libboost_thread.a(thread.o)

1
/usr/bin/c++ 是哪个编译器? - Mark B
1
@MarkB,c++ 是指向 g++ 的符号链接 :) - Max
4个回答

15

现在我有点尴尬。显然,我的boost库出了些问题。可能是我使用一种版本的gcc编译了它,之后又执行了“更新全部”操作。这可能会导致两个版本之间存在某些微小的不兼容性。但我可能完全错了。

我删除了所有的boost文件,并从Archlinux存储库中获取了最新的版本,这与官方发布的版本保持同步。现在它已经按照预期进行编译了。

我真是太傻了。:(


1
很高兴你解决了这个问题,但请注意这是GCC中某些配置实际存在的问题,仍然会发生。我今天在一个嵌入式应用程序中遇到了这个问题。 - Uli Köhler
使用相同版本的gcc/g++编译所有源代码文件是可行的。 - songhir

8
Linkonce部分(以及较新的comdat组概念)是编译器在不必担心链接输出文件中出现大量重复副本的情况下发出模板实例化和非内联代码函数的一种方式。例如,可能在2个或更多离散的对象文件中存在一个实例化的模板函数副本,链接器将选择其中一个并丢弃其他所有副本。为了使这种情况正确链接,每个副本定义并引用完全相同的符号非常重要。
然而,有时候会使用不同的编译选项(例如-g),或者当编译器选项在对象文件之间不同时(通常是共享库的情况),会定义额外的符号,或者从gcc的一个版本到另一个版本符号集合发生了变化(我怀疑这在你的情况下不太可能——编辑:嗯,看起来这可能是你的失误。很高兴你的问题解决了)。
在这种情况下可能发生的情况是,您具有从非Linkonce部分到某个特定Linkonce部分的一些(但不是全部)副本定义的符号的引用。如果链接器恰好丢弃了那个带有该符号的副本并保留了没有该符号的副本,则会出现此错误。
我建议您重新排列对象模块的链接顺序,尝试让链接器保留定义所需符号的部分;特别是根据它们之间的依赖关系将在错误消息中提到的对象文件移动到对象文件列表的结尾或开头(请记住,对象文件按照其命令行顺序从左到右链接)。

请查看我的编辑。:) 你至少部分正确——它确实解决了我的“未定义”错误。然而,我仍然遇到了这些巨大的“丢弃部分”错误。还有更多提示吗?我已尝试过不带调试符号进行编译,但结果相同。 - Max

4
这里是一个关于IT技术的翻译,内容如下:

这是我针对此问题提供的解决方案,不需要更改整个项目的构建标志:

假设您已经有了这个函数:

void foo() {/* ... */}

如果你的链接器产生以下错误消息:

`foo' referenced in section `...' of ...: defined in discarded section `.text' of ...

这意味着在编译/链接过程中,该函数被移除了。您可以使用GCC的__attribute__((used))函数属性来强制编译器不以任何方式清除函数:
__attribute__((used)) void foo() {/* ... */}

如果经常使用,我建议定义一个宏(也可以用于与其他编译器兼容):

#define USED __attribute__((used))
//Usage example
USED void foo() {/* ... */}

请注意,不建议将此属性添加到所有函数中 - 这可能会增加可执行文件/库的大小,因为许多未使用的函数将不会从最终编译中删除。
请参阅函数属性GCC文档页面

3

我怀疑问题的部分或全部在于链接行中库的顺序。一些链接器要求您按从最依赖到最不依赖、从左到右的顺序列出库,以便知道从每个库中提取哪些符号。首先,将 libnet 移动到 libboost_thread 之前,看看是否有改善(或至少有变化)。


你是正确的,问题的一部分是我链接了libboost_thread太晚 - 这些是undefined错误。然而,其他错误仍然存在。还有更多提示吗? - Max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接