C语言多线程编译出现undefined reference to `_imp__pthread_create‘

原因是没有添加线程库lpthread。

D:\>g++ a.cpp -o a
C:\Users\ADMINI~1\AppData\Local\Temp\ccabskXX.o:a.cpp:(.text+0x60): undefined reference to `_imp__pthread_create'
C:\Users\ADMINI~1\AppData\Local\Temp\ccabskXX.o:a.cpp:(.text+0x7e): undefined reference to `_imp__pthread_join'
c:/dev-cpp5.4.0api/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\ccabskXX.o: bad reloc address 0x0 in section `.ctors'
collect2.exe: error: ld returned 1 exit status

解决方法:添加编译命令-lpthread

D:\>g++ a.cpp -lpthread -o a

D:\>a
hello world

如果使用的是IDE,例如DEV C++,解决方法:工具-编译选项-编译时加入以下命令:-lpthread。

猜你喜欢

转载自blog.csdn.net/weixin_43731933/article/details/108876725