Difference between -lpthread and -pthread compilation opts

Hi Could someone explain what is the difference between compiling a program with -lpthread or -pthread ? I needed to use pthread_atfork() function in a c program. Only when compiling with -pthread that function is found in the library.

thanks Bogdan

Reply to
Bogdan
Loading thread data ...

When using -lpthread, did you remember to put the library specification after the object files on the command line?

--

Tauno Voipio
tauno voipio (at) iki fi
Reply to
Tauno Voipio

In GCC, the -pthread (aka -pthreads) option manages both the compiler preprocessor /and/ linker to enable compilation with Posix threads. The preprocessor will define/enable/use Posix threads versions of some macros (or perform conditional compilation to enable Posix threads logic), and the linker will specifically link the resultant object against libpthread

However, -lpthread simply tells the linker to attempt to resolve any external references against the libpthread library, in the same way that -lm tells the linker to attempt to resolve any external references against the libm library. For -lpthread, your code might not contain external references to libpthread, even if you wrote Posix thread code, because the critical macros haven't been switched on.

--
Lew Pitcher
Master Codewright & JOAT-in-training   | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
----------      Slackware - Because I know what I'm doing.         ------
Reply to
Lew Pitcher

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.