Reputation: 57
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?
Upvotes: 0
Views: 3057
Reputation: 159
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.
Upvotes: 4