Re: Problem in calling custom shared library from python

Thomas Heller <[email protected]> Thu, 08 Mar 2012 14:25:42 +0100
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Am 08.03.2012 12:07, schrieb Jarkko Vatjus-Anttila:
> Hello,
>
> I'm introducing myself to the ctypes python extension, and it really
> seems suiting my needs for the current project. I've been writing a test
> program to teach myself the secrets of ctypes, and it runs very well,
> apart from a slight problem.
>
> I'm running the code on 64-bit linux box. I have created a shared
> library, with a C API, as defined in my testlib_core.h:
>
> #ifndef testlib_core_h
> #define testlib_core_h
> int init(void);
> int store(int);
> int get(void);
> int clear(void);
> int destroy(void);
> #endif // testlib_core_h
>
> Behind of that API I have a c++ singleton class implementation which
> handles eventually all the requests made through the API. All this works
> well in basic principle. However, the problem is the methods for calling
> the API. I cannot use the names defined in the declarations, e.g.
> init(), store(), etc. If I call directly their mangled counterparts,
> like in this case _Z4initv() and _Z5storei(), those work just as I need.
>
> I figured that I would need the h2xml/xml2py conversion tools, but the
> problem there is that the .py output from xml2py is empty. The XML file
> itself is ok, and it contains all the information which exists in my
> shared lib, but still the only thing which exports into the .py file is
> only these lines:
>
> from ctypes import *
> __all__ = []

The h2xml/xml2py tools parse the header file and then look for the 
functions in the shared library, but they only look for the unmangled
names - they don't support C++ name mangling.

So, it would be best (if you can control the C/C++ source code) to 
change the header so that the functions are compiled with unmangled 
names. OTOH, the simple header file that you show above does not really
justify the use of h2xml/xml2py, instead you can trivially call the 
functions with ctypes.

Thomas


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/