Extending python - undefined symbol error on import

C

ch424

Hi there,

I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems
extending python in C:

The C code is below:

#include <Python.h>
#include "ni488.h"

static PyObject *
gpib_hello(PyObject *self, PyObject *args)
{
char *command;
int *secondarg;
// int sts;

if (!PyArg_ParseTuple(args, "|si", &command, &secondarg)) //both
arguments are optional
return NULL;

// TODO: Inserting code for the function here!

// FROM HERE
int Device = 0; /* Device unit descriptor*/
int BoardIndex = 0; /* Interface Index*/

int PrimaryAddress = 13; /* Pri addr of the device */
int SecondaryAddress = 0; /* Sec addr of the device */

Device = ibdev( /* Create a unit descriptor handle */
BoardIndex, /* Board Index*/
PrimaryAddress, /* Device pri addr */
SecondaryAddress, /* Device sec addr*/
T10s, /* Timeout setting = 10 seconds */
1, /* Assert EOI line at end of write */
0); /* EOS termination mode */

// TO HERE IS THE GPIB STUFF

return Py_BuildValue("s", "hello world");
}


static PyMethodDef gpibMethods[] = {
/* ... */
{"hello", gpib_hello, METH_VARARGS,
"Get Hello world returned."},
/* ... */
{NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC
initgpib(void)
{
(void) Py_InitModule("gpib", gpibMethods);
}

As you can see, it's the standard "spammodule" example, with "spam"
replaced with "gpib", an extra include (ni488) and that block of code
added. This compiles absolutely faultlessly (even no warnings!) using
the python script:

from distutils.core import setup, Extension

module1 = Extension('gpibmodule',
sources = ['gpibmodule.c'])

setup (name = 'gpibmodule',
version = '0.0.1',
description = 'This is the gpib package',
ext_modules = [module1])

However, when I open up the python command line, and type "from gpib
import *" or "import gpib" I get "ImportError: /usr/.../gpibmodule.so:
undefined symbol: ibdev" -- but I know it's defined in the ni488.h
file, especially as I can use this code from actual C programs without
problems. The ni488.h file in in the right place to be used for C
compilation.

So, the question is, what did I type wrong? Why is python examining
that c function embedded in another, surely it should only be
interested in providing arguments and getting returned values, and what
the c function does is its own bussiness?

Any help would be hugely appreceated!

Thanks

Alex
 
D

Daniel Dittmar

ch424 said:
However, when I open up the python command line, and type "from gpib
import *" or "import gpib" I get "ImportError: /usr/.../gpibmodule.so:
undefined symbol: ibdev" -- but I know it's defined in the ni488.h
file, especially as I can use this code from actual C programs without
problems. The ni488.h file in in the right place to be used for C
compilation.

I guess ibdev is *declared* in ni488.h and you'll have to add the lib
where it is *defined*. You'll probably have to add a
libraries = ['gpip']
to setup.py.

Daniel
 
J

John Machin

ch424 wrote:
[snip]
However, when I open up the python command line, and type "from gpib
import *" or "import gpib" I get "ImportError: /usr/.../gpibmodule.so:
undefined symbol: ibdev" -- but I know it's defined in the ni488.h
file, especially as I can use this code from actual C programs without
problems. The ni488.h file in in the right place to be used for C
compilation.

So, the question is, what did I type wrong? Why is python examining
that c function embedded in another, surely it should only be
interested in providing arguments and getting returned values, and what
the c function does is its own bussiness?

Daniel has already given you what to do to get over your immediate
problem. I'd just like to add a couple of mindset adjustments to get you
over the next few stiles.

The message ".... gpibmodule.so: undefined symbol: ibdev" is being
reported by Python, but this problem is detected by an operating-system
specific loader. What it is trying to tell you is that gpibmodule.so
contains a call to a function named ibdev, but the loader can't find
ibdev. This is nothing to do with extending Python; if you had written a
stand-alone program in C, and made the same mistake [not providing the
name of the library containing ibdev], you would have got a similar
message either from the linker or later from the loader.

Neither Python nor the loader is "examining" ibdev, the loader can't
even find it, and Python has no way of knowing that it exists (and
doesn't and shouldn't care) even if the loader could find it! It is the
job of *your* code to interface with the ibdev routine.

Another slant on all this: the problem doesn't exist on the boundary
between Python and your extension module; it exists on the boundary
between your module and a 3rd party function called by your module.

HTH,
John
 
C

ch424

Sweet! It works! *dances*

Thank you so much -- and for the explanation! For anyone searching for
this, I had to change the respective lines to:

module1 = Extension('gpibmodule',
libraries = ['gpibapi'],
sources = ['gpibmodule.c'])

just as Daniel said.

Thanks again,

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top