本文介绍了我如何正确地包裹在Xamarin.iOS本地C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想本机C(不客观C)库绑定到Xamarin.iOS。

I am trying to bind a native c (not objective c) library to Xamarin.iOS.

我的项目,该项目由.c和.h文件正在建设中的X code细的。实际.H和项目不包含任何功能.M,我只需要使用C定义的例程。

My project that consists of .c and .h files is building fine in XCode. Actual .h and .m of the project do not contain any functions, I only need to use the c-defined routines.

轰文件,包含所需的方法定义是这样的:

The h file, that contains the required method definition looks like this:

#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的定义吗?哪里是这个过程记录?我需要为我的特殊情况的教程。

I've build it into a fat binary, but the specification in Xamarin Documentation is not clear. Given I am binding a native library, do I need a Binding project in my solution. Anyway I need a wrapper call to put my DLLImport definitions in? Where is this process documented? I need a tutorial for my particular situation.

P.S。目的是Sharpie笔生成一个空的绑定类,如果我喂它在我的.h文件中,可能是因为它不是Objective-C的。

P.S. Objective Sharpie is generating an empty binding class if I "feed" it my .h file, probably since it is not objective-c.

推荐答案

由于它是C code,你应该使用类似的来生成C#绑定。

Since it is C code you should use something like PInvoke Interop Assistant to generate the C# bindings.

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) ;

}

没有ObjectiveC结合项目需要C库。刚刚建立的本地库,并将其与导入DLL添加到您的项目。

No ObjectiveC binding project is needed for C libraries. Just build the native lib and add it to your project with the Dll imports.

这篇关于我如何正确地包裹在Xamarin.iOS本地C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:48