[英]Create C/C++ library in Xcode and import it in C# .NETCore
我正在嘗試在Xcode中編寫一個C / C ++動態庫,將其編譯為.dylib
庫包(或任何你稱之為)和[DLLImport]
它在.NET Core中。
關於原因的一點背景:一家中國公司為我們開發設備,為了將他們的設備集成到我們的軟件中,他們用Borland C ++編寫了一個演示庫來測試集成並確定了解決方案。
現在我想知道我們是否有可能使用.NET Core或Xamarin將用Xcode編寫的C ++庫導入到我們的應用程序中。
現在我是C / C ++的新手,我對微軟提供的跨平台解決方案有點新意。 但根據這個github問題, DLLImport
應該適用於Mac。 現在我想知道如何。
因此,我盡最大努力編寫C ++庫:
ApiFunc.h
#ifndef ApiFuncH
#define ApiFuncH
double mean(double x, double y);
typedef void (*SignalHandler)(int signum);
typedef int (*OPEN_IMAGE_FILE)(char*);
extern OPEN_IMAGE_FILE open(char *FileName);
extern SignalHandler signal(int signum, SignalHandler handler);
class TAPIFunc
{
public:
int OpenImageFile(char *FileName);
};
#endif
ApiFunc.cpp
#pragma hdrstop
#include "ApiFunc.h"
double mean(double x, double y){
return x * y;
}
int TAPIFunc::OpenImageFile(char *FileName)
{
return 5;
}
只是嘗試一些不同的方法...所以這編譯成libtestmachw.dylib
我將其導入我的.NET Core控制台應用程序:
[DllImport("libtestmachw.dylib", EntryPoint = "mean")]
private static extern double mean(double x, double y);
[DllImport("libtestmachw.dylib")]
private static extern int OPEN_IMAGE_FILE(string fileName);
[DllImport("libtestmachw.dylib")]
private static extern int OpenImageFile(string fileName);
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("Let's try to communicate with a Mac dylib library");
Console.WriteLine("We are now going to invole function 'mean' of libtestmacw.dylib");
try
{
double result = mean(2, 4);
Console.WriteLine("yes, we made it! Result:" + result);
}
catch (Exception e)
{
Console.WriteLine("Opes that didn't work!");
Console.WriteLine(e);
}
Console.WriteLine("We are now going to invole function 'OPEN_IMAGE_FILE' of libtestmacw.dylib");
try
{
int result = OPEN_IMAGE_FILE("SomeFile.png");
Console.WriteLine("yes, we made it! Result:" + result);
}
catch (Exception e)
{
Console.WriteLine("Opes that didn't work!");
Console.WriteLine(e);
}
Console.WriteLine("We are now going to invole function 'OpenImageFile' of libtestmacw.dylib");
try
{
int result = OpenImageFile("SomeFile.png");
Console.WriteLine("yes, we made it! Result:" + result);
}
catch (Exception e)
{
Console.WriteLine("Opes that didn't work!");
Console.WriteLine(e);
}
Console.ReadLine();
}
運行它時,在Mac上,我得到一個System.EntryPointNotFoundException
無法在DLL“libtestmachw.dylib”中找到名為“OpenImageFile”的入口點。
我只是想測試我是否可以在.NETCore應用程序中導入函數,從他們開始我可以指示中國公司將他們的代碼編譯成.dylib
。 誰能幫助我或指出我正確的方向?
這個Microsoft頁面顯示它是可能的,所以我猜我在c / c ++方面做錯了什么? https://docs.microsoft.com/en-us/dotnet/standard/native-interop
你不能調用C ++函數,只能調用C函數。 此外,這些函數需要使用標准C約定導出,因此在C ++中,您可能還需要添加extern "C"
。
您的示例中的問題是TAPIFunc::OpenImageFile
是無法導入的C ++函數。
例如, .cpp
文件中定義的此函數可以在.NET Core中使用:
extern "C" int32_t Bridge_OpenFile(char* filename) {
// you can do some C++ calls here
return 0;
}
運用
[DllImport("my.dylib")]
private static extern int Bridge_OpenFile([MarshalAs(UnmanagedType.LPStr)]string filename);
另一個技巧: DllImport
可以和testmachw
一起使用,因為CoreCLR將自動添加lib
和.dylib
作為探測的一部分,這樣你就可以擁有相同的C#代碼,它將匹配lib{foo}.dylib
, lib{foo}.so
和{foo}.dll
取決於運行的平台。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.