无法在Mac上运行在Ubuntu上编译的共享库

时间:2016-07-04 02:06:10

标签: python c macos ubuntu ctypes

我在ubuntu机器上编译了一个cpp文件,并使用以下命令生成了链接器:

g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o

然后我在python中加载了libfoo.so链接器文件,如下所示。

from ctypes import *
lib = cdll.LoadLibrary('./lib/cppFunctions/libfoo.so')

然后我可以在ubuntu上使用带有python的cpp文件中的函数。

然而,当我尝试在我的Mac上加载.so文件(libfoo.so)时,我 得到以下错误。

OSError: dlopen(./lib/cppFunctions/libfoo.so, 6): no suitable image found.  Did find:
    ./lib/cppFunctions/libfoo.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00

完全错误:

File "Resume.py", line 7, in <module>
    from PhraseRecommender import *
  File "/Users/aerin/Documents/bitbucket_BYOR/PhraseRecommender.py", line 4, in <module>
    from SingleSentenceRecord import * #TODO: What is the right way to import.
  File "/Users/aerin/Documents/bitbucket_BYOR/SingleSentenceRecord.py", line 4, in <module>
    from ByorFunctions import *
  File "/Users/aerin/Documents/bitbucket_BYOR/ByorFunctions.py", line 29, in <module>
    lib = cdll.LoadLibrary('./lib/cppFunctions/libfoo.so')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
    return self._dlltype(name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(./lib/cppFunctions/libfoo.so, 6): no suitable image found.  Did find:
    ./lib/cppFunctions/libfoo.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00

如何在Mac上运行.so文件?

0 个答案:

没有答案