我正在做一个简单的程序,其中从Python调用C ++函数。我在Raspberry Pi中使用Raspbian OS运行此代码。这里我使用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,但它没有用
答案 0 :(得分:0)
请参阅:&#34;在Linux上,find_library()尝试运行外部程序(/ sbin / ldconfig,gcc和objdump)以查找库文件。它返回库文件的文件名&#34;, 尝试将/ usr / local / lib添加到/etc/ld.so.conf新行,然后添加sudo ldconfig 链接:http://blog.csdn.net/wolfzhaoshuai/article/details/46520371(中文)
按照这个,我解决了类似的问题。