g++ 链接静态库命令应该放在最后

昨天编译去年写的FloorServer,居然一堆错误:

chu@chu-laptop:/media/E/work/github/FloorServer/FloorServer$ make
g++ -g -lboost_thread -lboost_date_time main.cpp -o FloorServer
In file included from WorkThread.h:14:0,
from Server.h:15,
from main.cpp:2:
Message.h:18:8: 警告: #endif 指示的末尾有多余的标识符 [默认启用]
In file included from Server.h:13:0,
from main.cpp:2:
SocketManager.h: 在构造函数‘SocketManager::SocketManager()’中:
SocketManager.h:23:12: 警告: 扩展初始值设定列表只在 -std=c++0x 或 -std=gnu++0x 下可用 [默认启用]
/tmp/cclzUKo9.o: In function `pre_exit(int, int)':
/media/E/work/github/FloorServer/FloorServer/debug.h:18: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/debug.h:22: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/debug.h:37: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `CheckConfigure()':
/media/E/work/github/FloorServer/FloorServer/main.cpp:29: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/main.cpp:44: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o:/media/E/work/github/FloorServer/FloorServer/main.cpp:52: more undefined references to `boost::this_thread::get_id()' follow
/tmp/cclzUKo9.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:67: undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cclzUKo9.o: In function `interruption_checker':
/usr/include/boost/thread/pthread/thread_data.hpp:96: undefined reference to `boost::detail::get_current_thread_data()'
/tmp/cclzUKo9.o: In function `TaskQueue::Push(int)':
/media/E/work/github/FloorServer/FloorServer/TaskQueue.h:92: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/TaskQueue.h:96: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/TaskQueue.h:104: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `SocketManager::UpdateActiveTime(int)':
/media/E/work/github/FloorServer/FloorServer/SocketManager.h:33: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/SocketManager.h:38: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o:/media/E/work/github/FloorServer/FloorServer/Listener.h:37: more undefined references to `boost::this_thread::get_id()' follow
/tmp/cclzUKo9.o: In function `ThreadPool::StopAll()':
/media/E/work/github/FloorServer/FloorServer/WorkThread.h:167: undefined reference to `boost::thread::join()'
/media/E/work/github/FloorServer/FloorServer/WorkThread.h:169: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `Server::DestoryServer(Server*)':
/media/E/work/github/FloorServer/FloorServer/Server.h:45: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/Server.h:52: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `Server::Run()':
/media/E/work/github/FloorServer/FloorServer/Server.h:62: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `Server::Stop()':
/media/E/work/github/FloorServer/FloorServer/Server.h:71: undefined reference to `boost::this_thread::get_id()'
/media/E/work/github/FloorServer/FloorServer/Server.h:73: undefined reference to `boost::thread::join()'
/media/E/work/github/FloorServer/FloorServer/Server.h:77: undefined reference to `boost::thread::join()'
/media/E/work/github/FloorServer/FloorServer/Server.h:78: undefined reference to `boost::this_thread::get_id()'
/tmp/cclzUKo9.o: In function `bool boost::condition_variable_any::timed_wait<boost::unique_lock<boost::mutex> >(boost::unique_lock<boost::mutex>&, boost::posix_time::ptime const&)':
/usr/include/boost/thread/pthread/condition_variable.hpp:148: undefined reference to `boost::this_thread::interruption_point()'
/tmp/cclzUKo9.o: In function `thread<boost::_bi::bind_t<void, boost::_mfi::mf0<void, Worker>, boost::_bi::list1<boost::_bi::value<Worker*> > > >':
/usr/include/boost/thread/detail/thread.hpp:204: undefined reference to `boost::thread::start_thread()'
/tmp/cclzUKo9.o: In function `thread<void (Listener::*)(), Listener*>':
/usr/include/boost/thread/detail/thread.hpp:252: undefined reference to `boost::thread::start_thread()'
/tmp/cclzUKo9.o: In function `thread<void (SocketManager::*)(), SocketManager*>':
/usr/include/boost/thread/detail/thread.hpp:252: undefined reference to `boost::thread::start_thread()'
/tmp/cclzUKo9.o: In function `void boost::checked_delete<boost::thread>(boost::thread*)':
/usr/include/boost/checked_delete.hpp:34: undefined reference to `boost::thread::~thread()'
/tmp/cclzUKo9.o: In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::iso_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::basic_ostream<char, std::char_traits<char> >&)':
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
/tmp/cclzUKo9.o: In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::iso_extended_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::basic_ostream<char, std::char_traits<char> >&)':
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
/tmp/cclzUKo9.o: In function `~thread_data':
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cclzUKo9.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf0Iv13SocketManagerEENS2_5list1INS2_5valueIPS6_EEEEEEEE[typeinfo for boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, SocketManager>, boost::_bi::list1<boost::_bi::value<SocketManager*> > > >]+0x8): undefined reference to `typeinfo for boost::detail::thread_data_base'
/tmp/cclzUKo9.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf0Iv8ListenerEENS2_5list1INS2_5valueIPS6_EEEEEEEE[typeinfo for boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, Listener>, boost::_bi::list1<boost::_bi::value<Listener*> > > >]+0x8): undefined reference to `typeinfo for boost::detail::thread_data_base'
/tmp/cclzUKo9.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf0Iv6WorkerEENS2_5list1INS2_5valueIPS6_EEEEEEEE[typeinfo for boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, Worker>, boost::_bi::list1<boost::_bi::value<Worker*> > > >]+0x8): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld 返回 1
make: *** [FloorServer] 错误 1

从错误信息看,是boost thread库没有找到,导致链接失败。

但是经过确认,系统中的boost库是没有问题的,libboost_thread.a是导出了相应的符号的:

chu@chu-laptop:/media/E/work/github/FloorServer/FloorServer$ nm /usr/lib/libboost_thread.a |grep get_id
000014b0 T _ZN5boost11this_thread6get_idEv
00000440 T _ZNK5boost6thread6get_idEv
chu@chu-laptop:/media/E/work/github/FloorServer/FloorServer$ nm /usr/lib/libboost_thread.a |grep thread_data_base
00000000 W _ZN5boost23enable_shared_from_thisINS_6detail16thread_data_baseEED1Ev
00000000 W _ZN5boost23enable_shared_from_thisINS_6detail16thread_data_baseEED2Ev
00000000 n _ZN5boost23enable_shared_from_thisINS_6detail16thread_data_baseEED5Ev
00002bc0 T _ZN5boost6detail16thread_data_baseD0Ev
00002a70 T _ZN5boost6detail16thread_data_baseD1Ev
00002a70 T _ZN5boost6detail16thread_data_baseD2Ev

既然库没有问题,那么只可能是编译命令出错了。

对比其他项目的Makefile,发现这样像下面这样写能够编译通过:

g++ -o aimuserver main.o libcrypto.a -lboost_thread -lboost_date_time 

于是尝试修改编译命令,将链接库放在最后,输出目标放在最前:

g++ -g -o FloorServer main.cpp -lboost_thread -lboost_date_time

最后也编译通过了。

看来以后写Makefile时最好遵守:g++ -o output  source.cpp -lboost_thread 的形式。 

原文地址:https://www.cnblogs.com/chutianyao/p/3602453.html