【问题标题】:How do I correctly wrap native c library in Xamarin.iOS如何在 Xamarin.iOS 中正确包装本机 c 库
【发布时间】:2014-08-29 08:51:36
【问题描述】:

我正在尝试将本机 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__ */

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

附:如果我“输入”我的 .h 文件,Objective Sharpie 会生成一个空的绑定类,可能是因为它不是 Objective-c。

【问题讨论】:

    标签: c binding xamarin.ios xamarin native


    【解决方案1】:

    由于它是 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 导入将其添加到您的项目中。

    【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2014-08-26
    • 2017-04-13
    • 2013-06-28
    • 2016-12-23
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多