pythonic
pythonic

Reputation: 21665

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.

Upvotes: 5

Views: 325

Answers (1)

apmasell
apmasell

Reputation: 7153

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.

Upvotes: 2

Related Questions