4

I want to use pthread stuff in my static user library, but dependent projects won't link unless I add '-lpthread' to each project that uses it.

I would rather specify '-lpthread' in my own user library. Actually, I HAVE done that, but it doesn't do anything; I still need to add '-lpthread' to dependent projects, otherwise I get

Invoking: GCC C++ Linker
g++ <....>
/usr/bin/ld: /home/xxx/git/xxx/xxx.CUtil/Debug/libxxx.CUtil.a(EzyThread.o): undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'

IMO it defeats the purpose of my own user library, if I also have to include its dependencies in the projects using it; what if I decide to use another internal mechanism instead of pthread?

I've used

#pragma comment(lib, "SomeOtherStuff.lib")

in MS VC which does what I want - but I'm now in a gcc environment. I checked out #pragma comment(lib, "xxx.lib") equivalent under Linux? which seemed high on emotion and low on usable info. Is there either something similar in gcc, or some other way to avoid specifying '-lpthread' in each dependent project? I know that C isn't OOP but why make each dependency have to work out how the user library is implemented?

(Please don't say that something like the #pragma method is longer than '-lpthread'. Note that the #pragma, or equivalent mechanism in my user library, is typed once, but the '-lpthread' is needed potentially hundreds of times, and needs changing as many times if the underlying mechanism in the user library changes.)

brewmanz
  • 1,181
  • 11
  • 17

1 Answers1

0

Static libraries are really a tiny sauce on top of dumb archives of object files. In particular they do not track dependencies on other libraries.

Is there either something similar in gcc, or some other way to avoid specifying '-lpthread' in each dependent project? ... the '-lpthread' is needed potentially hundreds of times, and needs changing as many times if the underlying mechanism in the user library changes

In Unix world this is usually done at build system level (e.g. by setting LIBS environment variable appropriately if you deal with Autoconf).

Note that the #pragma, or equivalent mechanism in my user library, is typed once

Semantics of pragma isn't well specified though. To begin with, if there are two pragmas in different source files, which library should be linked first?

yugr
  • 19,769
  • 3
  • 51
  • 96