2

I recently took a class where we used pthreads and when compiling we were told to add -lpthread. But how come when using other #include <> statements for system header files, it seems that the linking of the object implementation code happens automatically? For example, if I just want to get the header file #include <stdio.h>, I don't need a -l option in compilation, the linking of that .o implementation file file just happens.

Clifford
  • 88,407
  • 13
  • 85
  • 165
John Doe
  • 113
  • 2
  • 7
  • I could not find proper dupes, but [here's an external link that explainds the difference between include files and libraries](https://www.geeksforgeeks.org/difference-header-file-library/). – DYZ Aug 15 '18 at 21:10
  • The nominated duplicate https://stackoverflow.com/questions/26562335/what-is-the-difference-between-include-and-link-when-linking-to-a-library answers the rather poorly worded question in the title (now modified by me), but the body indicates that this is in fact a different question. – Clifford Aug 15 '18 at 22:28

6 Answers6

1

For this file

#include <stdio.h>

int main() {
  return 0;
}

run

gcc -v -o simple simple.c

and you will see what, actually, gcc does. You will see that it links with libraries behind your back. This is why you don't specify system libraries explicitly.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
1

Basic Answer: -lpthreads tells the compiler/linker to link to the pthreads library.

Longer answer
Headers tell the compiler that a certain function is going to be available (sometimes that function is defined in the header, perhaps inline) when the code is later linked. So the compiler marks the function essentially available but later during the linking phase the linker has to connect the function call to an actual function. A lot of function in the system headers are part of the "C Runtime Library" your linker automatically uses but others are provided by external libraries (such as pthreads). In the case where it is provided by an external library you have to use the '-lxxx' so the compiler/linker knows which external library to include in the process so it gets the address of the functions correctly.

Hope that helps

Jim M.
  • 909
  • 1
  • 12
  • 29
1

A C header file does not contain the implementations of the functions. It only contains function prototypes, so that the compiler could generate proper function calls.

The actual implementation is stored in a library. The most commonly used functions (such as printf and malloc) are implemented in the Standard C Library (LibC), which is implicitly linked to any executable file unless you request not to link it. The threads support is implemented in a separate library that has to be linked explicitly by passing the -pthread option to the linker.

NB: You can pass the option -pthread to the compiler that will also link the appropriate library.

DYZ
  • 55,249
  • 10
  • 64
  • 93
1

The compiler links the standard C library libc.a implicitly so a -lc argument is not necessary. Pthreads is a system library but not a part of the C standard library, so must be explicitly linked.

If you were to specify -nolibc you would then need to explicitly link libc (or some alternative C library).

If all system libraries were linked implicitly, gcc would have to be have a different implementation for each system (for example pthreads is not a system library on Windows), and if a system introduced a new library, gcc would have to change in lock step. Moreover the link time would increase as each library were searched in some unknown sequence to resolve symbols. The C standard library is the one library the compiler can rely on to be provided in any particular implementation, so implicit linking is generally safe and a simple convenience.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

the -l option is for linking against an external library in this case libpthread, against system libraries is linked by default

http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html

C++: How to add external libraries

ralf htp
  • 9,149
  • 4
  • 22
  • 34
0

Some library files are are searched by default, simply for convenience, basically the C standard library. Linkers have options to disable this convenience and have no default libraries (for if you are doing something unusual and don't want them). Non-default libraries, you have to tell linker to use them.

There could be a mechanism to tell what libraries are needed in the source code (include files are plain text which is just "copy-pasted" at #include line), there's no technical difficulty. But there just isn't (as far as I know, not even as non-standard compiler extension).

There are some solutions to the problem though, such as pkg-config for unixy platforms, which tackle the problem of include and library files by providing the compiler and linker options for a library easily.

hyde
  • 60,639
  • 21
  • 115
  • 176