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

Aer*_*rin 5 c python macos ubuntu ctypes

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

g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o
Run Code Online (Sandbox Code Playgroud)

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

from ctypes import *
lib = cdll.LoadLibrary('./lib/cppFunctions/libfoo.so')
Run Code Online (Sandbox Code Playgroud)

然后我可以在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
Run Code Online (Sandbox Code Playgroud)

完整错误:

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
Run Code Online (Sandbox Code Playgroud)

如何让.so文件在mac上运行?