cht*_*tlp 18 c libraries gcc xubuntu
我最近安装了 XUbuntu 11.10 64 位,但在编译最简单的 pthread 示例时遇到问题。
这是代码pthread_simple.c:
#include <stdio.h>
#include <pthread.h>
main() {
pthread_t f2_thread, f1_thread;
void *f2(), *f1();
int i1,i2;
i1 = 1;
i2 = 2;
pthread_create(&f1_thread,NULL,f1,&i1);
pthread_create(&f2_thread,NULL,f2,&i2);
pthread_join(f1_thread,NULL);
pthread_join(f2_thread,NULL);
}
void *f1(int *x){
int i;
i = *x;
sleep(1);
printf("f1: %d",i);
pthread_exit(0);
}
void *f2(int *x){
int i;
i = *x;
sleep(1);
printf("f2: %d",i);
pthread_exit(0);
}
Run Code Online (Sandbox Code Playgroud)
这是编译命令
gcc -lpthread pthread_simple.c
结果:
lptang@tlp-linux:~/test/test-pthread$ gcc -lpthread pthread_simple.c /tmp/ccmV0LdM.o:在函数“main”中: pthread_simple.c:(.text+0x2c):对`pthread_create'的未定义引用 pthread_simple.c:(.text+0x46):对`pthread_create'的未定义引用 pthread_simple.c:(.text+0x57):对`pthread_join'的未定义引用 pthread_simple.c:(.text+0x68):对`pthread_join'的未定义引用 collect2: ld 返回 1 个退出状态
有谁知道是什么导致了问题?
Kar*_*son 31
在最新版本的gcc编译器中要求库遵循目标文件或源文件。
所以要编译它应该是:
gcc pthread_sample.c -lpthread
Run Code Online (Sandbox Code Playgroud)
通常虽然 pthread 代码是这样编译的:
gcc -pthread pthread_sample.c
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130586 次 |
| 最近记录: |