解决undefined reference to `pthread_atfork'/`pthread_detach'/`pthread_create'/`pthread_join'

在编译陈硕github上面的代码时,遇到了链接错误:

../Thread.o: In function `(anonymous namespace)::ThreadNameInitializer::ThreadNameInitializer()':

Thread.cpp:(.text+0x5f): undefined reference to `pthread_atfork'
../Thread.o: In function `muduo::Thread::~Thread()':
Thread.cpp:(.text+0x3f9): undefined reference to `pthread_detach'
../Thread.o: In function `muduo::Thread::start()':
Thread.cpp:(.text+0x50e): undefined reference to `pthread_create'
../Thread.o: In function `muduo::Thread::join()':
Thread.cpp:(.text+0x5ce): undefined reference to `pthread_join'

collect2: error: ld returned 1 exit status


在编译时已经加了-lpthread,还是有这个问题。

在这里找到了答案:http://stackoverflow.com/questions/2373109/what-library-to-be-to-be-used-to-avoid-undefined-reference-to-pthread-atfork

pthread_atfork是POSIX标准,在编译时要加上-pthread

-lpthread是老版本的gcc编译器用的,在新版本中应该用-pthread取代-lpthread

你可能感兴趣的:(Linux)