解决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 functionmuduo::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

posted @ 2021-01-12 12:03  Max_hhg  阅读(1635)  评论(0)    收藏  举报