【问题标题】:Error in opening shared object file in python ( OSError: cannot open shared object file: No such file or directory)在 python 中打开共享对象文件时出错( OSError: cannot open shared object file: No such file or directory)
【发布时间】: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


【解决方案1】:

请参阅:“在 Linux 上,find_library() 尝试运行外部程序(/sbin/ldconfig、gcc 和 objdump)来查找库文件。它返回库文件的文件名”, 尝试将 /usr/local/lib 添加到 /etc/ld.so.conf 新行,然后 sudo ldconfig 链接:http://blog.csdn.net/wolfzhaoshuai/article/details/46520371(中文)

按照这个,我解决了类似的问题。

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2018-05-22
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多