我试图在Python代码中使用一些C++方法,我使用的是^ {CD1>}库。我用一个简单的^ {< CD2}}编写了一个简单的C++代码,没有参数,还有一个简单的方法,叫做^ {CD3}},它的类型是^ {< CD4}}。我还编写了一个简单的python代码来导入这两个方法。我使用以下命令创建了两个链接库(一个.so
和一个.a
,因为我使用的是Debian):
g++ -o hello.so -shared -fPIC hello.cpp
然后使用export LD_LIBRARY_PATH=/the/path/to/the/linked/libraries/directory
。在
获取main
方法没有问题,但是当我试图得到printArgs
时,我得到了AttributeError
。下面是C++代码:
下面是Python代码:
from ctypes import *
helloInCPP_lib = cdll.LoadLibrary("hello.a")
print helloInCPP_lib
helloInCPPMain = helloInCPP_lib.main
print helloInCPPMain
helloInCPPPrint = helloInCPP_lib.printArgs
print helloInCPPPrint
我得到这个输出:
<CDLL 'hello.a', handle 9c45488 at b73d1e6c>
<_FuncPtr object at 0xb73e67e4>
Traceback (most recent call last):
File "testHelloInCPP.py", line 9, in <module>
helloInCPPPrint = helloInCPP_lib.printArgs(None)
File "/usr/lib/python2.6/ctypes/__init__.py", line 366, in __getattr__
func = self.__getitem__(name)
File "/usr/lib/python2.6/ctypes/__init__.py", line 371, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /etc/linked_libraries/hello.a: undefined symbol: printArgs
我还尝试了cdll.LoadLibrary("hello.so")
和/或{
我在VMWare工作站和Python2.6上使用了32位的Debian。在
使用^{} 声明{}:
顺便说一句,您应该传递一个字符串(c_char_p),而不是
^{pr2}$None
:关于
argtypes
,见Specifying the required argument types (function prototypes)。在相关问题 更多 >
编程相关推荐