Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python调用C函数-未定义符号错误_Python_C_Gcc_Ctypes - Fatal编程技术网

从Python调用C函数-未定义符号错误

从Python调用C函数-未定义符号错误,python,c,gcc,ctypes,Python,C,Gcc,Ctypes,我试图从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.

我试图从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
成功了,然后:

$ 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)

你需要和图书馆链接。根据,使用
-lgclib-lgclibo
。我刚刚注意到您的链接顺序错误。它应该是
gcc-shared-fPIC-Wl,-soname,gtest-o gtest.so gtest.c-lgclib-lgclibo
。如果不告诉链接器,gtest.so还会如何引用ELF头中的依赖项?检查
readelf-d gtest.so
。您应该看到libgclib.so和libgclibo.so被引用为
(需要)
。我会检查,但我没有安装库。如果.so头中不需要
libgclib.so.0和libgclibo.so.0
,那么运行时链接器将不会加载它们,并且您将返回到使用未定义符号开始的位置。由于共享库已在过程中加载,运行时链接器将查找所需符号。当您重新启动Python时,情况并非如此,除非您手动预加载依赖项。在第二个场景中,第一次尝试加载库失败,但不会使进程处于中断状态。因此,第二次尝试使用一个新的构建(声明所需的依赖项)可以按预期工作。
$ 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)