问题:
在运用gcc thread.c命令进行多线程编译时,会出现错误
/usr/bin/ld: /tmp/cc4myIvL.o: in function `main':
thread.c:(.text+0x6c): undefined reference to `pthread_attr_setstacksize'
/usr/bin/ld: thread.c:(.text+0x100): undefined reference to `pthread_create'
/usr/bin/ld: thread.c:(.text+0x124): undefined reference to `pthread_create'
/usr/bin/ld: thread.c:(.text+0x150): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
错误提示是链接错误,这是由于libpthread.a不使系统默认的库,所以在编译时需要链接libpthread.a。
即在使用pthread_xxxx()系列函数时,需要链接该库。
具体解决办法:
运行: gcc thread.c -o thread -lpthread