7

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....

imacake
  • 1,703
  • 7
  • 18
  • 26
  • 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 Miller Apr 30 '12 at 21:20

1 Answers1

19

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:

  1. Cause your program to crash when moved to a machine with a different version of glibc
  2. Cause your existing program to crash when your current machine's glibc is upgraded, but your module is not re-linked against updated libpthread.a.

Spare yourself aggravation, and don't do that.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    :o oh my gosh oh my gosh, Good that you state it. But when its part of glibc,... /me is derped.. – imacake Apr 30 '12 at 21:41
  • 1
    Thanks. 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 – kiranpradeep Sep 09 '14 at 07: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. – Employed Russian Sep 10 '14 at 05: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. – kiranpradeep Sep 10 '14 at 15:49
  • 2
    @Kiran You are wrong in that `libc != GLIBC`. `GLIBC` (package) contains `libc`, `libpthread`, `librt`, `libcrypt`, `libresolv` (individual files) and more. Not every program links in all of these. – Employed Russian Sep 11 '14 at 03:08
  • Ok. Thanks. Didn't knew that. – kiranpradeep Sep 13 '14 at 05:20
  • What if I statically link to all of `glibc` libraries? Can I avoid _version incompatibility_ issues that way? – Paolo M Jul 27 '16 at 07:28
  • @EmployedRussian Okay what is the alternative? Because I need to change some functions in pthreads! – Behnam Aug 06 '16 at 00:22