pthread.h를 include 했는데도 undefined reference 에러가 날때

Problem


Pthread API를 이용하여 프로그래밍을 할 때, pthread.h를 include 했는데도 다음과 같은 오류가 나타나며 컴파일이 안되는 경우가 있다. 빡침… pthread.h를 찾지 못해서 생기는 오류이다.

/tmp/cctcT7AX.o: In function `main':
pthread.c:(.text+0x4e): undefined reference to `pthread_create'
pthread.c:(.text+0x5f): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

Solution


$ gcc source.c -o source -lpthread

gcc 컴파일 옵션에 -lpthread를 추가하면 말끔히 해결된다.

gcc의 컴파일 옵션을 잘 활용하자

-l 옵션은 헤더 파일을 탐색할 디렉토리를 지정해주는 옵션이다.

You've successfully subscribed to devkoriel
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.