How to call C function in shared library?

Kalle Svensson kalle at gnupung.net
Thu Mar 8 14:37:09 EST 2001


Sez Oleg Krivosheev:
> i already have shared library
> with simple function which takes double
> and returns double. Is it simple way 
> to call it directly from Python?

You could take a look at SWIG (http://www.swig.org/), or make your own
wrapper.  Something like (extremely untested):

/************  BEGIN **************/
#include "Python.h"
#include "myheader.h"

static PyObject *
wrap_myfunction(PyObject *self, PyObject *args)
{
    double arg, ret;
    if (!PyArg_ParseTuple(args, "d", &arg))
        return NULL;
    ret = myfunction(arg);
    return Py_BuildValue("d", ret);
}

/* Module initialisation stuff goes here...     */
/* Check http://www.python.org/doc/current/ext/ */
/* and http://www.python.org/doc/current/api/   */
/* for more information...                      */
/************  END **************/

> Or some python encapsulation for dlopen/dlsym/dlclose ?

There is the dl module:
http://www.python.org/doc/current/lib/module-dl.html

Peace,
  Kalle
-- 
Email: kalle at gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
[Not signed due to braindamage.  Blame Microsoft Outlook Express.]




More information about the Python-list mailing list