17

As I know, if I want to use pthread library in linux environment I must include pthread.h and compile the source code with -lpthread option. But I don't understand why I should compile with -lpthread option. I think the option is redundant... because I already declared to include pthread.h header file so that gcc links pthread library. Why does gcc not link pthread library file automatically by reading #include?

Thanks in advance.

David Johns
  • 1,201
  • 5
  • 15
  • 34
  • 4
    Related: http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling, you should be using `-pthread` too. – Mat Feb 17 '12 at 16:27

6 Answers6

29

Well linking and compilation are two separate phases.

You include the header pthread.h so that the compiler understands the data types & symbol names, which you use in your source files but are defined/declared in the pthread library header file.

You link to the pthread libray using -lpthread so that the linker can actually find those symbols in the pthread library during the linking stage.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 5
    So, why don't we have to include some library explicitly for linking when we use strcpy or printf? Is it because these are all in the C standard library and that is included by default, and anything outside that library must be explicitly linked? – master_latch Jan 13 '14 at 22:45
  • @master_latch: yes thats the precise reason. – Alok Save Jan 14 '14 at 02:46
6

Having #include <pthread.h> in your code doesn't link in the library; it only includes the header for compilation. That allows the compiler to see the various structures, function declarations, etc. included. Having -lpthread actually causes the linking to be done by the linker. So the include tells the compiler what's available, and the -lpthread actually allows the program to call the functions within the library at runtime.

Dan Fego
  • 13,644
  • 6
  • 48
  • 59
3

Because GCC doesn't do auto-linking of libraries triggered by header inclusion (as opposed to MSVC, or so I've been told).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    There is indeed a `#pragma` option to link in a library in MSVC, but it's very rarely used (and generally considered bad practice). – James M Feb 17 '12 at 16:27
3

The header file just declares what the pthread functions are and how they should be called. -lpthread links to the library itself, containing the actual functions.

The compiler has no idea how you're going to resolve the functions in pthread.h. You might want to use a static library, the one provided by the system, some compatible implementation - heck, you might implement them yourself in another source file. It's up to the linker, and doesn't concern the compiler.

James M
  • 18,506
  • 3
  • 48
  • 56
1

Pthread.h header file is included in the posix thread program but you need -lpthread while compiling because it links it with the library of pthread NOTE: -lpthread -lpcap all are the switches with gcc compiler that can link particular library in our source code. (lpthread means "link pthread" library)

Rahul Raina
  • 3,322
  • 25
  • 30
1

By including the header files you tell the compiler which functions he's going to see. But if these functions are in an external library, like the pthread functions, you need to link this library to your program so it can actually access those functions. That's what -lpthread is doing.

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
DarkDust
  • 90,870
  • 19
  • 190
  • 224