我正在做一个简单的程序,其中从Python调用c++函数。我在有树莓派操作系统的树莓派上运行这个代码。这里我使用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上可以工作,但是在树莓派上它给出的错误如下:
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
我已经尝试过发布的问题:无法打开共享对象文件:没有这样的文件或目录,但它没有工作
参见:"在Linux上,find_library()尝试运行外部程序(/sbin/ldconfig, gcc和objdump)来查找库文件。它返回库文件的文件名。尝试将/usr/local/lib添加到/etc/ld.so.conf新行,然后sudo ldconfig链接:http://blog.csdn.net/wolfzhaoshuai/article/details/46520371
接着,我解决了类似的问题。