是否可以为给定的静态库人为地诱导目标文件提取?

Is it possible to artificially induce object file extraction for a given static library?

我最近在阅读 this answer 并注意到用户必须以正确的顺序 link 静态库似乎不方便。

是否有一些标志或 #pragma 我可以在编译我的库时传递给 gcc 以便我的库的目标文件将始终包含在内?

更具体地说,我希望用户可以在另一个依赖它的库之前 link 我的静态库,而不是像对象一样以未解析的符号结束在 linker 行中明确指定的文件是。

特别是,我正在寻找库的用户不需要做任何特殊事情的解决方案,只需要像 link 任何其他库一样传递 -lMylibrary

Is there some flag or #pragma I can pass to gcc

没有

I want it to be case that the user can link my static library before another library that depends on it, and not wind up with unresolved symbols, in the same way that object files which are explicitly specified on the linker line are.

将您的 "library" 作为单个目标文件发送。换句话说,而不是:

ar ru libMyLibrary.a ${OBJS}

使用:

ld -r -o libMyLibrary.a ${OBJS} 

In particular, I am looking for solutions where the user of the library does not need to do anything special, and merely needs to pass -lMylibrary the way they would link any other library.

您可以将目标文件命名为 libMyLibrary.a。我相信链接器会使用通常的规则搜索它,但是当它找到它时,它会发现这是一个目标文件,并以此对待它,尽管它是 "misnamed"。这应该至少在 Linux 和其他 ELF 平台上有效。我不确定它是否适用于 Windows.