0

Why do when we use pthread.h library function in a c program, to compile it we have to write
gcc program.c -pthread Why doesn't gcc program.c works. Although the simple reason could be
that it includes the pthread library when we specify the -pthread tag. But then we don't pass
any such argument when we include other header files. Is it because that pthread is an
external library than what's available from within C?

pensee
  • 47
  • 4
  • 2
    Does this answer your quesiton? https://stackoverflow.com/questions/2127797/significance-of-pthread-flag-when-compiling – KamilCuk Dec 10 '20 at 09:20
  • Another good dupe: https://stackoverflow.com/questions/41267121/pthread-lpthread-and-minimal-dynamic-linktime-dependencies – Petr Skocik Aug 28 '21 at 08:19

1 Answers1

4

The functions in libraries such as stdlib.h and stdio.h have implementations in libc.so, which is linked into your executable by default (as if -lc were specified).

Libraries such as math.h and pthread.h are not included in libc.so and hence have to be linked separately. This can be done by passing the -lm and -lpthread arguments respectively.

gcc program.c will actually compile perfectly but the linker will not find the required function definitions used and will hence throw an error.

Sam
  • 151
  • 4