【发布时间】:2014-05-23 11:25:10
【问题描述】:
我正在做一个从 Python 调用 C++ 函数的简单程序。我在具有 Raspbian OS 的 Raspberry Pi 中运行此代码。在这里,我使用 ctypes 从 python 调用 C++。这是我的代码 test.cpp:
#ifdef __cplusplus
extern "C"
#endif
class cppmax
{
int num1;
int num2;
int max(num1,num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
};
我的另一个python代码是test.py:
from ctypes import *
cmax = cdll.LoadLibrary('./test.dll').max
cmax.argtypes = [c_int, c_int] # arguments types
cmax.restype = c_int # return type, or None if void
a = int(raw_input("Enter 1st no"))
b = int(raw_input("Enter 2nd no"))
print "Your answer is :", cmax(a,b)
这些代码在 Ubuntu 上运行,但在 Raspberry Pi 中却出现以下错误:
Traceback (most recent call last)
File "test.py", line 3, in <module>
cmax=cdll.LoadLibrary('/home/pi/calling+cpp/test.dll').max
File "/usr/lib/python2.7/ctypes/_init_.py", line 365, in _init_
self._handle = _dlopen(self._name, mode)
OSError: /home/pi/calling_cpp/test.dll: cannot open shared object file: No such file or directory
对于编译 C++,我使用了如下命令:
g++ -Wall test.cpp -shared -o test.dll
我已经尝试过发布问题:cannot open shared object file: No such file or directory,但没有成功
【问题讨论】:
-
我不确定这是否可行,但您是否尝试过将项目文件夹添加到库路径,并加载
'test.dll'而不是通过相对路径?我认为这是规范的做法。 -
我尝试使用命令:
export LD_LIBRARY_PATH=/home/pi/calling_cpp:$LD_LIBRARY_PATH,但没有成功。 -
你有什么进展吗?我有同样的问题,无法解决。
标签: python c++ dll raspberry-pi ctypes