My program is built as a loader and many modules which are shared libraries. Now one of those libraries uses pthreads and it seems its bound to the module dynamically (loaded on startup). Now it'd be simplier if i could force pthreads to be linked into the module file. GCC on linux, how do i do? I guess a libpthread.a is necessary....
-
In what instance do you have to do anything other than include the pthread header to get it to build? Do you have a source example? Is this method of compiling an absolute must?– Adam MillerApr 30, 2012 at 21:20
1 Answer
While linking libpthread.a
into a shared library is theoretically possible, it is a really bad idea. The reason is that libpthread
is part of glibc
, and all parts of glibc
must match exactly, or you'll see strange and un-explainable crashes.
So linking libpthread.a
into your shared library will:
- Cause your program to crash when moved to a machine with a different version of
glibc
- Cause your existing program to crash when your current machine's
glibc
is upgraded, but your module is not re-linked against updatedlibpthread.a
.
Spare yourself aggravation, and don't do that.
-
1:o oh my gosh oh my gosh, Good that you state it. But when its part of glibc,... /me is derped..– imacakeApr 30, 2012 at 21:41
-
1Thanks. Like the answer. A query though - If libpthread is part of libc/glibc, why is it provided as a separate library. Almost all libs links to libc and then, what was then the need of libpthread Sep 9, 2014 at 7:27
-
@Kiran "Why is it provided as a separate library" -- because many programs do not need threads, and linking in
libpthread
imposes unnecessary overhead on such programs. Sep 10, 2014 at 5:53 -
Sorry. I wasn't clear enough with my query. What I understood from your answer is libpthread is subset of glibc/libc. That implies, any program which links to libc/glibc, can using multi-threading with out linking to libpthread. By telling "libpthread is part of glibc" - it appears like, if libpthread is A then libc is equal to A+X. So overhead of pthread is imposed on all programs linking to libc. I feel my understanding of your answer is wrong. But not sure where I am wrong. Sep 10, 2014 at 15:49
-
2@Kiran You are wrong in that
libc != GLIBC
.GLIBC
(package) containslibc
,libpthread
,librt
,libcrypt
,libresolv
(individual files) and more. Not every program links in all of these. Sep 11, 2014 at 3:08