I am creating a shared object library which will be LD_PRELOADed with my program. In that shared library, I also want to use some variables from my program. What is the way of declaring such variables. Note that shared object library is compiled separately from my program.

1 Answer
1

Yes. You must link your program with --export-dynamic to make the symbol table of the program accessible to the libraries opened. If you wish to control exactly which symbols are available and using libtool for linking, you can use parameters like -export-symbols-regex to specify which are available. If the symbols required by the library are not available when the program loads, it will fail with an undefined symbol. Some platforms require slightly different link flags (especially Windows). Consider using libtool to make this easier if you are not already.