2014-07-08 19 views
1

我正在嘗試將本地c(不是目標c)庫綁定到Xamarin.iOS。如何在Xamarin.iOS中正確包裝本地c庫

我的項目由.c和.h文件組成,在XCode中構建得很好。項目的實際.h和.m不包含任何函數,我只需要使用c定義的例程。

的.h文件,包含所需的方法定義是這樣的:

#ifdef __cplusplus 
extern "C" { 
#endif /* __cplusplus */ 

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <string.h> 

int sprec_flac_encode(const char *wavfile, const char *flacfile); 

#ifdef __cplusplus 
} 
#endif /* __cplusplus */ 

#endif /* !__SPREC_FLAC_ENCODER_H__ */ 

我已經建設成脂肪二元,但Xamarin文檔規範不明確。鑑於我綁定本地庫,我的解決方案中是否需要綁定項目?無論如何,我需要一個包裝調用將我的DLLImport定義放入?這個過程在哪裏被記錄?我需要一個關於我的特殊情況的教程。

P.S.目標Sharpie生成一個空綁定類,如果我「喂」它我的.h文件,可能因爲它不是objective-c。

回答

3

因爲它是C代碼,所以應該使用類似於PInvoke Interop Assistant的東西來生成C#綁定。

public partial class NativeMethods 
{ 
    [DllImport("<libnamehere>", 
     EntryPoint="sprec_flac_encode", 
     CallingConvention = CallingConvention.Cdecl)] 
    public static extern int FlacEncode(
     [MarshalAs(UnmanagedType.LPStr)] string wavfile, 
     [MarshalAs(UnmanagedType.LPStr)] string flacfile) ; 

} 

否C語言庫需要ObjectiveC綁定項目。只需構建本機庫並將其添加到您的項目與Dll導入。

+0

謝謝。你怎麼知道'const char *'被轉換成了'[MarshalAs(UnmanagedType.LPStr)]'?你能告訴我在哪裏可以讀到它嗎? –

+0

以下是編組參考:http://msdn.microsoft.com/en-us/library/s9ts558h(v=vs.110).aspx – SKall