在学习Linux程序设计POXIS线程的时候,编译一个段简单的代码,就出现下面的错误:
1 /tmp/cc2FQJTh.o:在函数‘main’中: 2 1-thread.c:(.text+0x29):对‘pthread_create’未定义的引用 3 1-thread.c:(.text+0x67):对‘pthread_join’未定义的引用 4 collect2: 错误: ld 返回 1
可是在我的代码中已经包含了pthread.h,这是什么回事呢,百度一下,发现网上大部分的解决办法是在gcc编译命令加上选项:lpthread。
1 gcc -o pthread -lpthread pthread.c
但我照着网上的方法试试,发现还是不行,错误提示与原来相同。后来才发现是我自己错了,我是在目标代码文件前加上-lpthread。但实际的做法应该是在代码文件后面添加次选项。
1 finlay@finlay-Lenovo-G470:~/文档/程序设计/Linux程序设计/12章$ gcc 1-thread.c -lpthread 2 #成功! 3 finlay@finlay-Lenovo-G470:~/文档/程序设计/Linux程序设计/12章$ gcc -lpthread -Wall 1-thread.c 4 #失败! 5 /tmp/ccE6dosW.o:在函数‘main’中: 6 1-thread.c:(.text+0x29):对‘pthread_create’未定义的引用 7 1-thread.c:(.text+0x67):对‘pthread_join’未定义的引用 8 collect2: 错误: ld 返回 1
但是还有一个什么有趣的现象,如下:
1 finlay@finlay-Lenovo-G470:~/文档/程序设计/Linux程序设