Calling C function from Python - undefined symbol error

1.4k Views Asked by At

I am trying to call a C function from the Galil gclib from Python. I am getting an undefined symbol OSError in Python so suspect a problem with my gcc command. Especially since I don't understand all the arguments to gcc.

This is my file gtest.c

#include "gclibo.h" //by including the open-source header, all other headers are pulled in.                                                              

GReturn test()
{
    GReturn rc;
    char buf[1024]; //traffic buffer                                                                                                                 

    GCon g; //var used to refer to a unique connection                                                                                               

    rc = GOpen("192.168.0.174 -d", &g); //Open a connection to Galil, store the identifier in g.                                                     
    printf("rc: %d\n", (int) rc);

    rc = GInfo(g, buf, sizeof(buf));        
    printf("rc: %d\n", (int) rc);
    printf("info: %s\n", buf); //Print the connection info                                                                                           

    rc = GClose(g); //Don't forget to close!                                                                                                         

    return rc;
}

I compiled it with:

gcc -shared -Wl,-soname,gtest -o gtest.so -fPIC gtest.c

In Python 2.7, I get:

In [1]: import ctypes
In [2]: gtest = ctypes.CDLL('/home/mitch/wt1/gtest.so')
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-7-008ab6422af2> in <module>()
----> 1 gtest = ctypes.CDLL('/home/mitch/wt1/gtest.so')

/usr/lib/python2.7/ctypes/__init__.pyc in __init__(self, name, mode, handle,     use_errno, use_last_error)
    363 
    364         if handle is None:
--> 365             self._handle = _dlopen(self._name, mode)
    366         else:
    367             self._handle = handle

OSError: /home/mitch/wt1/gtest.so: undefined symbol: GInfo

Tried:

gcc -shared -Wl,-soname,gtest -o gtest.so -lgclib -lgclibo -fPIC gtest.c

which succeeds and then:

$ ldd gtest.so

  linux-vdso.so.1 =>  (0x00007ffd076eb000)
  libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8284d7b000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f8285342000)
0

There are 0 best solutions below