undefined reference to `pthread_create' in Linux
2021-03-31 20:58
1516 查看
undefined reference to `pthread_create' in Linux
情况说明
在进行Linux系统多线程编程学习时,使用了
pthread_create这一系统调用。但是在使用gcc编译时出现了报错,具体如下:
代码:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> /* this data is shared by the threads */ int sum; /* threads call this function */ void *runner(void *param); int main(int argc, char *argv[]) { pthread_t tid; pthread_attr_t attr; if (argc != 2){ fprintf(stderr, "usage: a.out <integer value>\n"); return -1; } if (atoi(argv[1]) < 0){ fprintf(stderr, "%d must be >= 0\n", atoi(argv[1])); return -1; } /* get default attributes */ pthread_attr_init(&attr); /* create the thread */ pthread_create(&tid, &attr, runner, argv[1]); /* wait for the thread to exit */ pthread_join(tid, NULL); printf("the sum is %d\n", sum); return 0; } void *runner(void *param){ int i, upper = atoi(param); sum = 0; for(i = 0; i <= upper; i++){ sum += i; } pthread_exit(0); }
报错:
/usr/bin/ld: /tmp/ccAmXOUU.o: in function `main': 4-9-pthreads.c:(.text+0xcd): undefined reference to `pthread_create' /usr/bin/ld: 4-9-pthreads.c:(.text+0xde): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status
解决办法
在编译时,加上所需要的库,即
pthread
gcc -o test test.c -pthread
参考文章
https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux
相关文章推荐
- FreeModbus undefined reference to `pthread_create'
- 在linux下编译线程程序undefined reference to `pthread_create'
- pthread.h 的 undefined reference to `pthread_create'
- undefined reference to `pthread_create' collect2: ld returned 1 exit status
- undefined reference to `pthread_create'
- [Linux] undefined reference to `pthread_create'
- Linux下eclipse编译C/C++程序遇到 undefined reference to `pthread_create'的异常解决办法
- undefined reference to 'pthread_create' 问题解决
- undefined reference to 'pthread_create'问题解决
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- undefined reference to `pthread_create'
- undefined reference to 'pthread_create'
- linux undefined reference to 'pthread_create'问题解决
- undefined reference to 'pthread_create'问题解决
- undefined reference to 'pthread_create'问题解决(转)
- Linux下undefined reference to ‘pthread_create’问题解决
- cmake :undefined reference to 'pthread_create',当CMake需要pthread库问题
- undefined reference to \'pthread_create\'问题解决
- undefined reference to `pthread_create'
- 编译错误----undefined reference to 'pthread_create'问题解决