我最近安装了64位XUbuntu 11.10,但是在编译最简单的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); 
}这是编译命令
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退出状态
有谁知道是什么原因造成的?
                  是的,我使用了前置环境。现在应该正确显示。
                
                
                  
                    —
                    chtlp 2012年
                    
                  
                
              
                  顺便说一句,请使用编译
                
                  
                    —
                    马太福音
                    
                  
                
              -Wall,您缺少标题。(和SR_是正确的。)
                
#include <pthread.h>