'gcc: undefined reference to function

I know there have been many similar questions, but I have no choice but to post this question because I have not found an answer to my problem even after hours of googling. I'd appreciate your understanding.

I linked with gcc as follows.

gcc myprogram.c csapp.c -o a.out

csapp.c defines the functions used in myprogram.c. Myprogram.c is including csapp.h.

The following error is occurring:

/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_create':
csapp.c:(.text+0x121e): undefined reference to `pthread_create'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_cancel':
csapp.c:(.text+0x1257): undefined reference to `pthread_cancel'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_join':
csapp.c:(.text+0x129b): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_detach':
csapp.c:(.text+0x12d4): undefined reference to `pthread_detach'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_once':
csapp.c:(.text+0x1343): undefined reference to `pthread_once'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Sem_init':
csapp.c:(.text+0x1370): undefined reference to `sem_init'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `P':
csapp.c:(.text+0x139f): undefined reference to `sem_wait'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `V':
csapp.c:(.text+0x13ce): undefined reference to `sem_post'
collect2: error: ld returned 1 exit status

These are definitely defined in csapp.c. Why do these errors occur?



Solution 1:[1]

You should compile and link with -pthread

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mathieu