在Linux中编译并安装好boost后使用boost_thread 库是报出一下错误:
g++ -o text2 text2.cpp -lboost_system -lboost_thread
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::detail::get_current_thread_data()’中:
thread.cpp:(.text+0x13d):对‘pthread_key_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::detail::set_current_thread_data(boost::detail::thread_data_base*)’中:
thread.cpp:(.text+0x17d):对‘pthread_key_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::thread::start_thread_noexcept()’中:
thread.cpp:(.text+0x1f3):对‘pthread_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::thread::start_thread_noexcept(boost::thread_attributes const&)’中:
thread.cpp:(.text+0x2c0):对‘pthread_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::detail::make_external_thread_data()’中:
thread.cpp:(.text+0xef2):对‘pthread_key_create’未定义的引用
thread.cpp:(.text+0xf0a):对‘pthread_setspecific’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::thread::detach()’中:
thread.cpp:(.text+0x1326):对‘pthread_detach’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘thread_proxy’中:
thread.cpp:(.text+0x1751):对‘pthread_setspecific’未定义的引用
thread.cpp:(.text+0x178f):对‘pthread_setspecific’未定义的引用
thread.cpp:(.text+0x182b):对‘pthread_key_create’未定义的引用
thread.cpp:(.text+0x184b):对‘pthread_key_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::thread::join_noexcept()’中:
thread.cpp:(.text+0x1dbc):对‘pthread_join’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::thread::do_try_join_until_noexcept(timespec const&, bool&)’中:
thread.cpp:(.text+0x1fbc):对‘pthread_join’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::this_thread::hiden::sleep_until(timespec const&)’中:
thread.cpp:(.text+0x21d1):对‘pthread_getspecific’未定义的引用
thread.cpp:(.text+0x2353):对‘pthread_getspecific’未定义的引用
thread.cpp:(.text+0x23db):对‘pthread_key_create’未定义的引用
thread.cpp:(.text+0x23fb):对‘pthread_key_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::this_thread::hiden::sleep_for(timespec const&)’中:
thread.cpp:(.text+0x2a6f):对‘pthread_getspecific’未定义的引用
thread.cpp:(.text+0x2aeb):对‘pthread_key_create’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::detail::get_current_thread_data()’中:
thread.cpp:(.text+0x156):对‘pthread_getspecific’未定义的引用
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libboost_thread.a(thread.o):在函数‘boost::detail::set_current_thread_data(boost::detail::thread_data_base*)’中:
thread.cpp:(.text+0x196):对‘pthread_setspecific’未定义的引用
collect2: error: ld returned 1 exit status
其原因是因为一些附件依赖的库没有连接上。
可以试试一下编译:
g++ -o text2 text2.cpp -lboost_system -lboost_thread -lpthread