如何在Xamarin.iOS中正确包装本机C库

Max*_*lov 3 c binding native xamarin.ios xamarin

我正在尝试将本机c(不是目标c)库绑定到Xamarin.iOS。

我的由.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__ */
Run Code Online (Sandbox Code Playgroud)

我已经将其构建为胖二进制文件,但是Xamarin文档中的规范尚不清楚。鉴于我正在绑定本机库,因此我的解决方案中是否需要一个Binding项目。无论如何,我需要一个包装器调用来放入DLLImport定义?该过程记录在哪里?我需要针对我的特殊情况的教程。

PS Objective Sharpie如果我将其“ .h”文件“馈入”,则会生成一个空的绑定类,这可能是因为它不是Objective-C。

SKa*_*all 5

由于它是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) ;

}
Run Code Online (Sandbox Code Playgroud)

C库不需要ObjectiveC绑定项目。只需构建本机库,然后使用Dll导入将其添加到您的项目中即可。