2017-06-14 71 views
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 

我相信這是我應該得到的。任何人對如何解決這個問題有什麼建議?

回答

3

Miniconda顯然是一個64位版本的Python。你的庫是32位的。 (i386的體系結構是32位;您應該看到64位庫的x86_64)。因此,Python正確報告「錯誤的體系結構」。

Recompile your library as a 64-bit binary並重試。

+0

嗨,羅布,謝謝你的回答。我做了重新編譯庫,並確保「-Px86_64」標誌顯示在選項中,但我仍然得到相同的錯誤。在重新編譯的庫上運行'lipo -info',我沒有看到輸出有任何變化,即我仍然將體系結構作爲i386。任何想法是怎麼回事? – kdheepak

+1

嘗試使用該鏈接上的其他解決方案 - 「直接運行ppcx64而不是fpc」? –

+0

這個其他的解決方案工作! – kdheepak