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

标签 c++ linux gcc linker static-linking

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

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

更具体地说,我希望用户可以在另一个依赖它的库之前链接我的静态库,而不是以未解析的符号结束,就像在上明确指定的目标文件一样链接器行是。

特别是,我正在寻找图书馆用户不需要做任何特殊事情,只需要通过 -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.

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

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。我相信链接器会使用通常的规则搜索它,但是当它找到它时,它会发现这是一个目标文件,并以此对待它,尽管它被“错误命名”。这至少应该适用于 Linux 和其他 ELF 平台。我不确定它是否适用于 Windows。

关于c++ - 是否可以为给定的静态库人为地诱导目标文件提取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274847/

相关文章:

linux - <linux/proc_fs.h> 中缺少 Ubuntu proc_root_driver

regex - 如何使用sed匹配月日年模式?

c - MinGW GCC 无法识别 MEMSTATUSEX?

在 gcc 中包含 .h 文件时编译 C 程序

c++ - 在子目录中调用 target_link_libraries 的替代方法

c++ - 测试是否存在左移运算符

c++ - 结构 (C) 中的 malloc 错误

excel - 使用 CSV 文件中的唯一模式过滤特定数据,并将该数据移动到同一行的新列

c++ - 用 nullptr 初始化

c++ - 使用 GCC 编译 C++ 程序