c++ - 在 python 中使用链接库中的方法时出现属性错误

标签 c++ python ctypes undefined-symbol

我正在尝试在我的 python 代码中使用一些 C++ 方法,并且我正在使用 ctypes 库。我使用一个不带参数的简单 main 和另一个名为 printArgs 的简单方法编写了一个简单的 C++ 代码,该方法采用 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<<"Hello\n";
    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<<"Hello\n";
}

顺便说一句,您应该传递一个字符串 (c_char_p),而不是 None:

...
helloInCPPPrint = helloInCPP_lib.printArgs
helloInCPPPrint.argtypes = [c_char_p]
print helloInCPPPrint("ABCDE")

关于argtypes,请参阅Specifying the required argument types (function prototypes) .

关于c++ - 在 python 中使用链接库中的方法时出现属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20866575/

相关文章:

c++ - 使用附加参数复制和移动分配

c++ - 在 CallGraphSCCPass 中使用 DominatorTreeWrapperPass

python - Caffe:绘制 CNN 网络

c++ - 从 C++ 修改 python 列表

c++ - Ctypes "symbol not found"用于 OSX 中的动态库

python - C++ DLL 返回从 Python 调用的指针

c++ - 在代码中使用 STLport 库的主要原因是什么?

python - 在 python 中测量 TLS 握手性能时间

python - 如何在 Python 中获取当前脚本的代码?

c++ - 断言失败(size.width>0 && size.height>0)