-   Programming (
-   -   dynamically load symbols of an executable from a library (python) (

ta0kira 03-31-2012 10:23 PM

dynamically load symbols of an executable from a library (python)
This pertains to embedding a python interpreter in a C program. This is all done in Linux.

edit: It turns out it's actually giving me a handle to the binary (I compared the handle given to that from a direct call to dlopen from the binary,) so it seems to be a matter of resolving the symbol (which has C linkage.)

I'm trying to get a handle for a running binary that links to my library using ctypes.cdll.LoadLibrary('') in python. The running binary is written in C/C++ and the python interpreter is invoked via a call to a function in my library; therefore, this call just gives me a handle to my own library. I know this because I can access the symbols in my library from the handle and not the symbols in the binary.

I want to do this so that I can define C functions in the binary with external linkage to allow me to have python bindings for those functions without having to explicitly write wrappers in C. Basically, it's a C++ library with an embedded python interpreter for configuration/debugging purposes, and I'd like to "extend" the interpreter's functionality from the source of the binary by merely defining a new C function. For example: My library handles data transfer, and during a debugging session I might want to reload data from a database. It would be nice if I could add a "refresh" C function in the binary that I could call from the debugging python session to have the binary refresh its display after the reload. This functionality doesn't belong in the library, however, since it's specific to the binary.

Is there a way to get the symbols of the main binary in this situation? If it can be done at the C level I might be able to write a simple replacement for ctypes.cdll if there's nothing in python that will handle it. Please note that I've already written numerous python bindings in C for functionality within the library (I use a python file to configure the library,) but the maintainers of the binary probably won't want to write new bindings.

Kevin Barry

ta0kira 04-01-2012 12:49 AM

I found a solution in the dlopen manpage. I added the --export-dynamic linker flag to the executable's build, and now I can access the C functions from the main binary in the python session with ctypes.cdll.LoadLibrary('').
Kevin Barry

All times are GMT -5. The time now is 02:06 AM.