Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-31-2012, 09:23 PM   #1
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 03-31-2012 at 11:50 PM.
Old 03-31-2012, 11:49 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
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


dlopen, dynamic loading, python, symbol resolution

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Save/Load data segment of Dynamically loaded Shared Object library vragukumar Programming 3 08-26-2010 11:29 AM
'nm' lists all symbols - including static library symbols painulyarun Programming 4 04-06-2010 04:04 AM
dynamically list all symbols from a shared library famsinyi Programming 2 09-09-2009 06:21 PM
How do you load dynamically a library from C? Hano Programming 4 06-07-2002 03:04 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration