Lazarus库在Python for OSX中使用ctypes
我在Lazarus有一个非常小的库,我无法在Mac上使用它的ctypes。我真的不明白我做错了什么,我希望有人能给我指出正确的方向 这是我在拉撒路的密码。在Linux机器(UbuntuVM)上编译时,一切正常。我可以创建一个Linux.so文件,并使用ctypes调用共享库Lazarus库在Python for OSX中使用ctypes,python,macos,delphi,ctypes,lazarus,Python,Macos,Delphi,Ctypes,Lazarus,我在Lazarus有一个非常小的库,我无法在Mac上使用它的ctypes。我真的不明白我做错了什么,我希望有人能给我指出正确的方向 这是我在拉撒路的密码。在Linux机器(UbuntuVM)上编译时,一切正常。我可以创建一个Linux.so文件,并使用ctypes调用共享库 library project1; {$mode delphi}{$H+} {$IFDEF Darwin} {$linkframework CoreFoundation} {$linkframework Carbon}
library project1;
{$mode delphi}{$H+}
{$IFDEF Darwin}
{$linkframework CoreFoundation}
{$linkframework Carbon}
{$ENDIF}
function SubStr(CString: PChar;FromPos,ToPos: Longint): PChar; cdecl;
var
Length: Integer;
begin
Length := StrLen(CString);
SubStr := CString + Length;
if (FromPos > 0) and (ToPos >= FromPos) then
begin
if Length >= FromPos then
SubStr := CString + FromPos - 1;
if Length > ToPos then
CString[ToPos] := #0;
end;
end;
exports
SubStr;
end.
下面是这方面的Python代码
import ctypes
lib = ctypes.CDLL('./libproject1.so')
lib.SubStr.argtypes = (ctypes.c_char_p, ctypes.c_int64, ctypes.c_int64)
lib.SubStr.restype = ctypes.c_char_p
lib.SubStr('HelloWorld', 1, 5)
# The output is 'Hello' as expected
然而,当我在我的Mac上测试同样的东西时,我得到以下错误
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-44-37971b70da86> in <module>()
1 import ctypes
2
----> 3 ctypes.CDLL('./libproject1.dylib')
/Users/$USER/miniconda2/lib/python2.7/ctypes/__init__.pyc in __init__(self, name, mode, handle, use_errno, use_last_error)
360
361 if handle is None:
--> 362 self._handle = _dlopen(self._name, mode)
363 else:
364 self._handle = handle
OSError: dlopen(./libproject1.dylib, 6): no suitable image found. Did find:
./libproject1.dylib: mach-o, but wrong architecture
我相信这就是我应该得到的。有人对如何解决这个问题有什么建议吗?Miniconda显然是Python的64位版本。你的库是32位的。(i386的体系结构是32位的;您应该看到x86_64的64位库。)因此,Python正确地报告了“错误的体系结构”
然后再试一次。嗨,罗布,谢谢你的回答。我确实重新编译了库,并确保“-Px86_64”标志显示在选项中,但是我仍然得到相同的错误。在重新编译的库上运行
lipo-info
时,我看不到输出中有任何更改,即我仍然将架构作为i386。知道发生了什么吗?在该链接上尝试另一种解决方案——“直接运行ppcx64而不是fpc”?
$ lipo -info libproject1.dylib
Non-fat file: libproject1.dylib is architecture: i386