我的任务是在 Linux 上从 dotnet core 调用 openzwave,但我在 dotnet core 加载我的 c++ 库时遇到了问题。基本上任何时候我接触 openzwave 库我都会得到一个 dll not found 异常。 这是我的 program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace MinOZWDotNet
{
public class Program
{
[DllImport("MinOZW")]
public static extern int Init();
[DllImport("MinOZW")]
public static extern int Free();
public static void Main(string[] args)
{
Console.WriteLine("Calling Init");
var val= Init();
Console.WriteLine($"retval= {val}");
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape)
{
Console.WriteLine("Exit");
break;
}
}
}
Console.WriteLine("Calling Free");
val = Free();
Console.WriteLine($"retval = {val}");
}
}
}
这里是 .so 的一个版本
#ifdef __GNUC__
#define EXPORT extern "C"
#define CC
#else
#define EXPORT extern "C" __declspec (dllexport)
#define CC __stdcall
#endif
#ifdef __GNUC__
#include <stdlib.h>
#include <unistd.h>
#include "Defs.h"
#else
#include "Windows.h"
#endif
EXPORT int CC Init() {
return 0;
}
EXPORT int CC Free() {
return 0;
}
这是给我错误的版本
#ifdef __GNUC__
#define EXPORT extern "C"
#define CC
#else
#define EXPORT extern "C" __declspec (dllexport)
#define CC __stdcall
#endif
#ifdef __GNUC__
#include <stdlib.h>
#include <unistd.h>
#include "Defs.h"
#else
#include "Windows.h"
#endif
#include "Options.h"
#include "Manager.h"
#include "Driver.h"
#include "Node.h"
#include "Group.h"
#include "Notification.h"
#include "value_classes/ValueStore.h"
#include "value_classes/Value.h"
#include "value_classes/ValueBool.h"
#include "platform/Log.h"
using namespace OpenZWave;
EXPORT int CC Init() {
Manager::Create();
return 0;
}
EXPORT int CC Free() {
Manager::Destroy();
return 0;
}
openzwave 和这个库都在预期的路径上。 N.B 在 Windows 上编译时这一切都有效。 openZwave on github
最佳答案
您是否尝试过在 C 程序中使用您的库?也许 OpenZWave 有一些在 linux 上缺少的依赖项......
关于c++ - 系统.DllNotFoundException : Unable to load DLL with dotnet core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871068/