简体   繁体   English

GCC 将共享库链接到可执行文件

[英]GCC Linking a shared library against an executable

I have the following problem.我有以下问题。 I have a shared library, which is just a bunch of translation units linked together so when I compile that shared library I won't get any linker error (undefined references, even though I might have).我有一个共享库,它只是一堆链接在一起的翻译单元,所以当我编译那个共享库时,我不会得到任何链接器错误(未定义的引用,即使我可能有)。

The shared library gets loaded dynamically from an executable which also contains the exports which my shared library is using (The references used in my library are resolved at runtime).共享库从一个可执行文件动态加载,该可执行文件还包含我的共享库正在使用的导出(我的库中使用的引用在运行时解析)。

The main problem is that I want the undefined reference warnings so I can fix them statically instead of waiting the application to crash.主要问题是我想要未定义的引用警告,以便我可以静态修复它们,而不是等待应用程序崩溃。

I read somewhere that I can pass "-Wl,--no-undefined" to gcc so I can get these errors back, indeed it worked but it also gave me all the undefined references of the executable's exports.我在某处读到我可以将“-Wl,--no-undefined”传递给gcc,这样我就可以恢复这些错误,确实有效,但它也给了我可执行文件导出的所有未定义引用。 I want to filter these warnings just to the scope of my translation units.我想将这些警告过滤到我的翻译单元的范围内。

Is this possible?这可能吗? If not, how can I define reference to a executable which has exports for a shared library.如果没有,我如何定义对具有共享库导出的可执行文件的引用。

you can try linking the library & main program with -Wl,-z,now .您可以尝试使用-Wl,-z,now链接库和主程序。 that should make the runtime ldso resolve all references immediately and throw an error if none are found.这应该使运行时 ldso 立即解析所有引用,如果没有找到则抛出错误。

otherwise, i'm not seeing an option off hand in the linker manual to say "allow this ELF to satisfy symbols, but don't actually list it as a DT_NEEDED".否则,我在链接器手册中没有看到一个选项说“允许这个 ELF 满足符号,但实际上不要将它列为 DT_NEEDED”。

you could try using -Wl,--no-undefined and parsing the output with a script so you can filter out symbols you know will be satisfied by the main program.您可以尝试使用-Wl,--no-undefined并使用脚本解析输出,以便过滤掉您知道主程序会满足的符号。

another option might be to label all the symbols you know the main program provides with __attribute__((weak)) and then still use -Wl,--no-undefined .另一种选择可能是用__attribute__((weak))标记您知道主程序提供的所有符号,然后仍然使用-Wl,--no-undefined the weak symbols won't be reported as an error.弱符号不会被报告为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM