gcc trick
来源:互联网 发布:winhex数据恢复 编辑:程序博客网 时间:2024/05/30 22:43
Question
I have recently installed XUbuntu 11.10 64bit, but I am having problem compiling the most simple pthread example.
Here is the code 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);
}
And here is the compile command
gcc -lpthread pthread_simple.c
The results:
lptang@tlp-linux:~/test/test-pthread$ gcc -lpthread pthread_simple.c
/tmp/ccmV0LdM.o: In function `main':
pthread_simple.c:(.text+0x2c): undefined reference to `pthread_create'
pthread_simple.c:(.text+0x46): undefined reference to `pthread_create'
pthread_simple.c:(.text+0x57): undefined reference to `pthread_join'
pthread_simple.c:(.text+0x68): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
Does anyone know what's causing the problem?
Answer:
In the latest versions of gcc
compiler require that libraries follow the object or source files.
So to compile this it should be:
gcc pthread_sample.c -lpthread
Normally though pthread code is compiled this way:
gcc -pthread pthread_sample.c
KarlsonH2012-03-04 13:45:32
Thanks, it does the trick!
- gcc trick
- trick
- Trick
- trick
- 让gcc支持成员函数模板的trick
- 让gcc支持成员函数模板的trick
- dellphi trick
- wxpython trick
- java trick
- Just Trick!
- google trick
- pointer trick
- Tiny trick.
- python trick
- linux trick
- shell trick
- Card Trick
- shell trick
- jquery属性
- PHP中使用AMQP消息队列
- ffmpeg 保存流媒体
- linux top VIRT RES SHR SWAP DATA内存参数详解
- Effective Java 第 22 条:优先考虑静态成员类
- gcc trick
- ORACLE SEQUENCE用法
- 啊啊啊啊啊啊啊啊啊啊啊xcode 让人抓狂啊!!!!
- jquery筛选
- cocos2d 程序优化
- XCode快捷键
- hdu 1398
- NAT 典型组网
- uva 10305