gpt4 book ai didi

c - 如何在 Xamarin.iOS 中正确包装 native c 库

转载 作者:太空狗 更新时间:2023-10-29 16:12:23 25 4
gpt4 key购买 nike

我正在尝试将 native c(非 objective-c )库绑定(bind)到 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 文档中的规范并不清楚。鉴于我正在绑定(bind) native 库,我的解决方案中是否需要绑定(bind)项目。无论如何,我需要一个包装器调用来放入我的 DLLImport 定义吗?这个过程在哪里记录?我需要针对我的特殊情况的教程。

附言如果我将 .h 文件“提供”给 Objective Sharpie,它会生成一个空的绑定(bind)类,这可能是因为它不是 objective-c。

最佳答案

因为它是 C 代码,你应该使用像 PInvoke Interop Assistant 这样的东西生成 C# 绑定(bind)。

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 绑定(bind)项目。只需构建 native 库并使用 Dll 导入将其添加到您的项目中。

关于c - 如何在 Xamarin.iOS 中正确包装 native c 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com