728x90
반응형
질문 : Linux에서 pthread_create에 대한 정의되지 않은 참조
https://computing.llnl.gov/tutorials/pthreads/ 에서 웹에서 다음 데모를 선택했습니다.
#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); }
하지만 내 컴퓨터 (Ubuntu Linux 9.04 실행)에서 컴파일하면 다음 오류가 발생합니다.
corey@ubuntu:~/demo$ gcc -o term term.c term.c: In function ‘main’: term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’ /tmp/cc8BMzwx.o: In function `main': term.c:(.text+0x82): undefined reference to `pthread_create' collect2: ld returned 1 exit status
pthread_create
함수가 있어야하는 pthread.h
포함되어 있기 때문에 이것은 나에게 의미가 없습니다. 무슨 일이 일어나고 있는지 아이디어가 있습니까?
답변
Linux의 경우 올바른 명령은 다음과 같습니다.
gcc -pthread -o term term.c
일반적으로 라이브러리는 명령 줄에서 소스와 개체를 따라야하며 -lpthread
는 "옵션"이 아니라 라이브러리 사양입니다. libpthread.a
만 설치된 시스템에서
gcc -lpthread ...
링크에 실패합니다.
출처 : https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux
728x90
반응형
'개발관련 > Linux' 카테고리의 다른 글
OS X : Linux의 wget과 동일 (0) | 2021.06.18 |
---|---|
파일이있는 폴더를 Unix / Linux의 다른 폴더로 복사하는 방법 (0) | 2021.06.18 |
트리 형태로 디렉토리 구조를 인쇄하는 Linux 명령 (0) | 2021.06.16 |
Linux 셸 스크립트에서 Yes/No/Cancel 입력을 요청하는 방법 (0) | 2021.06.15 |
Linux 디렉토리에서 재귀 적으로 파일 계산 (0) | 2021.06.14 |