Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share variable in a shared object library

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.

like image 220
pythonic Avatar asked Nov 04 '22 22:11

pythonic


1 Answers

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.

like image 103
apmasell Avatar answered Nov 07 '22 22:11

apmasell