System.DllNotFoundException:无法使用 dotnet core 加载 DLL

System.DllNotFoundException: Unable to load DLL with dotnet core

本文关键字:dotnet core 加载 DLL DllNotFoundException System      更新时间:2023-10-16

我的任务是从Linux上的dotnet core调用openzwave,我在加载c ++库时遇到了dotnet core的问题。基本上,每当我触摸openzwave库时,我都会得到一个dll未找到异常。这是我的程序.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和这个lib都走在预期的道路上。注意,当在Windows上编译时,这一切都有效。openZwave on github

您是否尝试过在 C 程序中使用您的库?也许 OpenZWave 有一些在 Linux 上缺少的依赖项......

解决了!.我做了一个叫做.so的c ++程序,它指出了我的问题:)

#include <iostream>
#include <dlfcn.h>
typedef int (*moo)();
int main()
{
  std::cout << "Hello World!" << std::endl;
  void* myso = dlopen("/home/mark/open-zwave/.lib/MinOZW.so", RTLD_NOW );

//  void* myso = dlopen("/home/mark/open-zwave/libopenzwave.so", RTLD_NOW);
  if (!myso){
    std::cout << "Failed to load lib " << dlerror() << std::endl;
    return 1;
  }
//  moo func =(moo) dlsym(myso, "Free");
//  func();
  dlclose(myso);
  return 0;
}

g++ -o main main.cpp -ldl
。/主要

世界您好!

无法加载库

libopenzwave.so.1.4:无法打开共享对象文件:没有这样的文件或目录

稍后一个快速符号链接,一切都:)