這段時間學習boost 的asio 編程,想編譯asio自帶的http/server的程序,無奈在網上根本找不到方法,只能本身摸索學習。html
登錄boost asio 的example 目錄,(我 boost 安裝在/opt目錄下)編程
cd /opt/boost_1_55_0/libs/asio/example/cpp03/http/server
編譯命令: 多線程
g++ *.cpp -lboost_system -lpthread -I/usr/local/include/boost -L/usr/local/lib -o main
我以前本身編譯了一次簡單的asio的程序,編譯命令不須要加 –lpthread,詳細參考博文:學習
http://www.cnblogs.com/chenfool/p/3719428.htmlspa
但編譯http/server的程序,不加 –lpthread 命令會出現以下錯誤:線程
/tmp/ccODZX6Y.o: In function `boost::asio::detail::posix_signal_blocker::posix_signal_blocker()':
server.cpp:(.text._ZN5boost4asio6detail20posix_signal_blockerC2Ev[_ZN5boost4asio6detail20posix_signal_blockerC5Ev]+0x44): undefined reference to `pthread_sigmask'
/tmp/ccODZX6Y.o: In function `boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()':
server.cpp:(.text._ZN5boost4asio6detail20posix_signal_blockerD2Ev[_ZN5boost4asio6detail20posix_signal_blockerD5Ev]+0x2a): undefined reference to `pthread_sigmask'
/tmp/ccODZX6Y.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
server.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x1d): undefined reference to `pthread_detach'
/tmp/ccODZX6Y.o: In function `boost::asio::detail::posix_thread::join()':
server.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x25): undefined reference to `pthread_join'
/tmp/ccODZX6Y.o: In function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
server.cpp:(.text._ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE[boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)]+0x24): undefined reference to `pthread_create'
collect2: ld returned 1 exit statuscode
能夠看到,錯誤信息中說asio 的庫中,有使用多線程的庫,因此在編譯時也必須同時聲明使用pthread的庫來編譯。server
此編譯命令同時也支持其他http/server?的編譯。htm
詳細本身看。blog