在 python 中使用链接库中的方法时出现属性错误
Attribute error while using methods from linked libraries in python
我正在尝试在我的python代码中使用一些C++方法,并且正在使用ctypes
库。我编写了一个简单的C++代码,使用一个没有参数的简单main
和另一个名为 printArgs
的简单方法,该方法采用 char *
类型的参数。我还编写了一个简单的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++代码:
#include <iostream>
using namespace std;
int printArgs(char * args_array){
for (int i = 0; i < 5; i++){
cout<<i<<"- "<<args_array[i]<<"n";
}
return 0;
}
int main(){
cout<<"Hellon";
return 0;
}
这是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")
和/或helloInCPPPrint = helloInCPP_lib.printArgs(None)
;在两种情况下都得到了相同的错误。知道吗?
我在VMWare工作站和Python 2.6上使用Debian 32位。
使用 extern "C"
声明printArgs
:
#include <iostream>
using namespace std;
extern "C" {
int printArgs(char * args_array);
}
int printArgs(char * args_array){
for (int i = 0; i < 5; i++){
cout<<i<<"- "<<args_array[i]<<"n";
}
}
int main(){
cout<<"Hellon";
}
顺便说一句,你应该传递一个字符串(c_char_p),而不是None
:
...
helloInCPPPrint = helloInCPP_lib.printArgs
helloInCPPPrint.argtypes = [c_char_p]
print helloInCPPPrint("ABCDE")
关于argtypes
,请参阅指定所需的参数类型(函数原型)。
我厌倦了通过使用@falsetru编译解决方案
g++ -O3 -shared -std=c++11 -fPIC code.cpp -o lib.so
但是不能没有对code.c进行这种小的修改,根据他们的答案Extern"C"中提供的链接:
#include <iostream>
using namespace std;
extern "C" int printArgs(char * args_array){
for (int i = 0; i < 5; i++){
cout<<i<<"- "<<args_array[i]<<"n";
}
}
相关文章:
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- CMake 错误 - 目标 foo INTERFACE_SOURCES属性包含在源目录中以前缀为前缀的路径
- 当警告为错误时使用 [[已弃用]] 属性 (-Werror)
- C++ 中常量属性的初始化构造函数错误
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- MFC 对话框属性表用法在 VS 2017 中产生错误,适用于 VS 2013
- pybind11:属性错误:尝试从 py 文件运行函数时,模块'XXX'没有属性'YYY'
- 创建新对象并立即为其设置属性时出现编译器错误
- Windows 脚本主机 - 错误 '800A01B6' 对象不支持此属性或方法
- accelerator.cu(8): 错误:属性"managed"在这里不适用?
- 我刚刚安装了Visual Studio,遇到了一些错误,特别是WindowsSDKDir属性未定义.可能找不到某些生成工
- 有没有办法将 vector<unsigned char> 插入 postgresql 表中,具有 bytea 属性,没有 UTF8 编码错误?
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- Arduino / esp8266:错误:'Pchr 不允许部分属性
- 在 python 中使用链接库中的方法时出现属性错误
- GNU Radio OOT 模块属性错误:"模块"对象没有属性"MME_cpp"
- 属性错误:"模块"对象没有属性"func",仅通过C++调用
- 属性错误: 'Foo'对象没有属性'max'
- 类成员属性错误