1
我在Lazarus中有一个非常小的库,我无法在Mac上将它与ctypes一起使用。我真的不明白自己做错了什么,我希望有人能指出我正确的方向。在OSX中使用Python中的ctypes的Lazarus库
这是我在拉撒路的代码。在Linux机器(Ubuntu VM)上编译时,一切正常。我可以创建一个Linux .so文件,并使用ctypes,我可以调用共享库。
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
我在mac上创建了一个新项目,并尝试使用同样的方法无济于事。
关于在这个dylib上使用lipo,我得到以下输出。
$ lipo -info libproject1.dylib
Non-fat file: libproject1.dylib is architecture: i386
我相信这是我应该得到的。任何人对如何解决这个问题有什么建议?
嗨,罗布,谢谢你的回答。我做了重新编译库,并确保“-Px86_64”标志显示在选项中,但我仍然得到相同的错误。在重新编译的库上运行'lipo -info',我没有看到输出有任何变化,即我仍然将体系结构作为i386。任何想法是怎么回事? – kdheepak
尝试使用该链接上的其他解决方案 - “直接运行ppcx64而不是fpc”? –
这个其他的解决方案工作! – kdheepak