Alex Rivera | Logout

-lpthread option of gcc

Asked 2012-02-17T16:24:15.407
18

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.

Edit
Report

1 Answer

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.

answered 2012-02-17T16:26:56.097

Your Answer