使用gcc,如何将库(-lpthread)包含到我自己的静态用户库中?

时间:2023-01-13 16:04:48

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.

我想在静态用户库中使用pthread,但是依赖项目不会链接,除非我向每个使用它的项目添加“-lpthread”。

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

我宁愿在我自己的用户库中指定'-lpthread'。实际上,我做过,但它什么也没做;我仍然需要向依赖项目添加“-lpthread”,否则我将得到

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?

在我看来,它违背了我自己的用户库的目的,如果我也必须在使用它的项目中包含它的依赖项;如果我决定使用另一个内部机制而不是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?

在MS VC中,我想做什么就做什么——但我现在处于gcc环境中。我在Linux下检查了#pragma comments (lib,“xxx.lib”)等价句。这似乎是高情感和低可用信息。gcc中是否有类似的东西,或者其他方法来避免在每个依赖项目中指定“-lpthread”?我知道C不是OOP,但是为什么每个依赖项都必须计算出用户库是如何实现的呢?

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

(请不要说#pragma方法比'-lpthread'要长。请注意,在我的用户库中,#pragma或等效机制是一次输入的,但是“-lpthread”可能需要数百次,如果用户库中的底层机制发生变化,则需要多次更改。

1 个解决方案

#1


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

gcc中是否有类似的东西,或者其他方法来避免在每个依赖项目中指定“-lpthread”?…“-lpthread”可能需要数百次,如果用户库中的底层机制发生更改,则需要多次更改

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

在Unix世界中,这通常是在构建系统级别完成的(例如,如果处理Autoconf,则适当地设置LIBS环境变量)。

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

注意,我的用户库中的#pragma或等效机制只输入一次

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?

但是,并没有很好地指定pragma的语义。首先,如果在不同的源文件中有两个实用程序,应该首先链接哪个库?

#1


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

gcc中是否有类似的东西,或者其他方法来避免在每个依赖项目中指定“-lpthread”?…“-lpthread”可能需要数百次,如果用户库中的底层机制发生更改,则需要多次更改

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

在Unix世界中,这通常是在构建系统级别完成的(例如,如果处理Autoconf,则适当地设置LIBS环境变量)。

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

注意,我的用户库中的#pragma或等效机制只输入一次

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?

但是,并没有很好地指定pragma的语义。首先,如果在不同的源文件中有两个实用程序,应该首先链接哪个库?