【发布时间】:2019-11-26 16:43:42
【问题描述】:
我在 Xamarin 中有一个用于 mac 的控制台项目,并且想使用来自外部库的 c++ 函数。但不幸的是,当我尝试像这样使用 DLLIMPORT (c#) 时:
[DllImport('path to the library')]
static extern double sum(double x, double y);
public static void Main(string[] args)
{
Console.WriteLine(sum(2,2));
}
我得到 System.DLLNotFoundException。但是它适用于系统库:
[DllImport("/usr/lib/libSystem.dylib")]
static extern IntPtr dlopen(string path, int mode);
这是我所有的代码:
1) main.cpp(来自 lib.dylib 库):
extern "C" double sum(double x, double y) {
return x + y;
}
2) Program.cs(来自 Xamarin C# 项目):
using System;
using System.Runtime.InteropServices;
namespace Project {
class MainClass {
[DLLImport("/full/path/to/lib.dylib")]
static extern double sum(double x, double y);
public static void Main(string[] args) {
Console.WriteLine(sum(2,2));
Console.ReadKey();
}
}
}
我使用 XCode 创建库 (.dylib),但不确定我是否做对了所有事情。
【问题讨论】: