pthread线程操作函数库不是Linux系统默认的库,编译时需要指定使用线程库libpthread.a,所以在使用pthread_create创建线程时,在gcc编译时要加-lpthread参数选项。
root@ubuntu:/home/forlinuxsmb/threads# gcc threadid.c
/tmp/cc2ZWjnD.o: In function `main':
threadid.c:(.text+0x3c6): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
如:
root@ubuntu:/home/forlinuxsmb/threads# gcc threadid.c -lpthread
root@ubuntu:/home/forlinuxsmb/threads# ./a.out
root@ubuntu:/home/forlinuxsmb/threads# gcc threadid.c -lpthread -o threadid
root@ubuntu:/home/forlinuxsmb/threads# ./threadid