今天在CentOS上用CodeBlocks写了个简单pthread的代码:
#include <pthread.h>
void *thread(void *vargp);
int main(int argc, char **argv)
{
pthread_t tid;
pthread_create(&tid,NULL,thread,NULL);
pthread_join(tid,NULL);
exit(0);
}
void *thread(void *vargp)
{
printf("Hello,world!\n");
return NULL;
}但是死活编译不了,总是报undefined reference to pthread_create这个错误。最后还是在stackexchange上找到了答案
原来最新版本的gcc编译pthread代码需要跟-pthread这个参数:
gcc -pthread hello.c -o hello
这样就可以了,CodeBlocks设置:
Settings → Compiler → Linker settings → Other link options
另外如果用CodeBlocks需要手动的添加链接文件,不然也会报类似的错误。
记录一下。