我试图从Python中调用Galil gclib中的C函数。我在Python中得到一个未定义的符号OSError,所以怀疑我的gcc命令有问题。特别是因为我不了解gcc的所有论据。
这是我的文件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;
}
我编译它:
gcc -shared -Wl,-soname,gtest -o gtest.so -fPIC gtest.c
在Python 2.7中,我得到:
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
尝试:
gcc -shared -Wl,-soname,gtest -o gtest.so -lgclib -lgclibo -fPIC gtest.c
成功然后:
$ 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)