简体   繁体   中英

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. 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 . that should make the runtime ldso resolve all references immediately and throw an error if none are found.

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".

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.

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 . the weak symbols won't be reported as an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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