gcc는 pthread에 연결할 수 없습니까? pthread_exit(0); } void *f2(int *x){ int i;

최근에 XUbuntu 11.10 64bit를 설치했지만 가장 간단한 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 개의 종료 상태를 리턴했습니다.

누구든지 문제의 원인을 알고 있습니까?



답변

최신 버전의 gcc컴파일러에서는 라이브러리가 객체 또는 소스 파일을 따라야합니다.

따라서 이것을 컴파일하려면 다음과 같아야합니다.

gcc pthread_sample.c -lpthread

일반적으로 pthread 코드는 다음과 같이 컴파일됩니다.

gcc -pthread pthread_sample.c


답변

gcc -o exectable_namme pthread_sample.c -lpthread


답변

다음 명령을 사용하여 코드 컴파일

gcc filename.c -lpthread -lrt


답변