How to use a variable in a shared object library

I am creating a shared object library that will be LD_PRELOADed with my program. In this shared library, I also want to use some variables from my program. What is the way to declare such variables. Please note that the shared object library is compiled separately from my program.

+5
source share
1 answer

Yes. You must link your program with --export-dynamicso that the symbol table of the program is accessible to open libraries. If you want to precisely control which characters are available, and using them libtoolfor linking, you can use options such as -export-symbols-regexto indicate available. If the characters required by the library are not available when the program loads, it will fail with the undefined character. Some platforms require slightly different link flags (especially Windows). Think about using libtoolit to make it easier if you haven't already.

+2
source

All Articles