Subj : Re: why this program can be linked successfully with and without To : comp.programming.threads From : David Butenhof Date : Thu Jan 06 2005 08:48 pm Andy Yang wrote: > Just a simply program. But to my surprise, it can be linked > successfully with and without -lpthread option: > gcc test.c > or > gcc test.c -lpthread > > Both works fine. However, the two linked version show different > behavior. I am using Fedore Core 3. Many basic C runtimes (libc, glibc, etc) contain "stubs" for the pthread functions that allow you to link and run, but won't actually do anything. The mutex operations, for example, often return success without doing a thing, while pthread_create() usually fails. This is principally of use with thread-safe libraries, so they can be linked into main programs with or without threads. With threads, they "do their threadly things", and without threads they behave normally but without the extra overhead of synchronization. It does mean that you cannot link a library that USES threads into a main program that doesn't link libpthread -- you'll still resolve the (already loaded) stubs. Several systems have tried to avoid this with runtime redirection mechanisms... but they, too, have their share of problems and complications. In the end, pretty much everyone has agreed that this static "switch" based on the linkage of the main program is the best solution. -- Dave Butenhof, David.Butenhof@hp.com HP Utility Pricing software, POSIX thread consultant Manageability Solutions Lab (MSL), Hewlett-Packard Company 110 Spit Brook Road, ZK2/3-Q18, Nashua, NH 03062 .