linux下undefined reference to 、`pthread_create’,undefined reference to `pthread_join’
源代码:
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
void *mythread(void *arg){
printf("%s\n", (char*)arg);
return NULL;
}
int main(int argc, char* argv[]){
pthread_t p1, p2;
int rc;
printf("main:begin\n");
rc = pthread_create(&p1, NULL, mythread, "A");
assert(rc == 0);
rc = pthread_create(&p2, NULL, mythread, "B");
assert(rc == 0);
rc = pthread_join(p1, NULL);assert(rc == 0);
rc = pthread_join(p2, NULL);assert(rc == 0);
printf("main:end\n");
}
~
编译出现的错误:
出现原因:pthread不是linux默认的库,无法找到pthread库中函数的入口地址,会链接失败。
解决方案:
gcc t1.c -o t1 -lpthread