Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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 找不到类型错误属性错误符号,OS X 10.7.5_Python_C++_Ctypes - Fatal编程技术网

Python 找不到类型错误属性错误符号,OS X 10.7.5

Python 找不到类型错误属性错误符号,OS X 10.7.5,python,c++,ctypes,Python,C++,Ctypes,我在C++上有一个简单的测试函数: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h> char fun() { printf( "%i", 12 ); return 'y'; } 并在python中将其与ctypes一起使用: from ctypes import cdll

我在C++上有一个简单的测试函数:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>

char fun() {
    printf( "%i", 12 );

   return 'y';
}
并在python中将其与ctypes一起使用:

from ctypes import cdll
from ctypes import c_char_p

lib = cdll.LoadLibrary('test.so')
hello = lib.fun
hello.restype = c_char_p

print('res', hello())
但是我得到了一个错误:

Traceback (most recent call last):   File "./sort_c.py", line 10, in <module>
    hello = lib.fun   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 366, in __getattr__
    func = self.__getitem__(name)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 371, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self)) 
AttributeError: dlsym(0x100979b40, fun): symbol not found
Traceback(最近一次调用last):文件“/sort_c.py”,第10行,在
hello=lib.fun File“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/_init____;.py”,第366行,在__
func=self.\uuuuGetItem\uuuuuuuuu(名称)文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/\uuuuuuuuuu init\uuuuuuuuuuuu.py”,第371行,在uuuuuuuu getitem中__
func=self.\u FuncPtr((名称或顺序,self))
AttributeError:dlsym(0x100979b40,乐趣):未找到符号
哪里有问题

使用:

Mac Os X 10.7.5和Python 2.7

你的第一个问题是C++名字的修改。如果在

文件上运行
nm
,则会得到如下结果:

nm test.so
0000000000000f40 T __Z3funv
                 U _printf
                 U dyld_stub_binder
如果在使用C++编译时将其标记为C样式:

#ifdef __cplusplus
extern "C" char fun()
#else
char fun(void)
#endif
{
    printf( "%i", 12 );

   return 'y';
}
nm
给出:

0000000000000f40 T _fun
                 U _printf
                 U dyld_stub_binder
您的第二个问题是python将因
分段错误而消亡:11
(在OSX上)。C++返回一个<代码> char < />代码,而在Python中则将它标记为指向char的指针。使用:

hello.restype = c_char
相反(将
import
语句更改为匹配)

编辑:正如@eryksun指出的,您不应该使用
gcc
,而应该使用
g++
。否则,将不会链接正确的C++运行时。要检查操作系统X,请执行以下操作:

otool -L test.so

<>(<代码> LDD < /C> >,在UNIX/Linux上常用的工具,不是用OS X分配)< /P>使用<代码> GCC < /C> >将C++程序链接到OS X中的C++运行时吗?我希望它需要
g++
。我知道在这个例子中,这不是什么问题,因为示例代码只是使用C标准库函数。@如果ErkkSun:<代码> GCC < /Cube >如果源文件有后缀<代码> .CPP ,编译它为C++。我在回答中没有提到它,因为有人对它(你的?)发表了评论。这是明显的C++,因为名称的条件与条件<代码>外部“C”/代码>(见正文)不同。<代码> GCC < /C>编译源为C++,但在Linux上它不与LIbSTDc++连接。所以,6,如果需要C++库,加载库失败。然后我意识到我一直在使用
g++
进行测试!我想我是不假思索地使用了正确的方法,而且我没有复制OP的编译行。我补充了一条评论。谢谢
otool -L test.so