Mark
Mark

Reputation: 1554

System.DllNotFoundException: Unable to load DLL with dotnet core

I've been tasked with calling openzwave from dotnet core on linux and i'm having issues with dotnet core loading my c++ library. Essentially any time i touch the openzwave library I get a dll not found exception. here's my 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}");

            }
        }
    }

heres a version of the .so which works

#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;
}

heres the version which is giving me the error

#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;
}

both openzwave and this lib are on the expected path. N.B when compiled on windows this all works. openZwave on github

Upvotes: 1

Views: 1917

Answers (2)

Mark
Mark

Reputation: 1554

Solved it!!. I made a c++ program which called the .so which pointed me to the issue :)

#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
./main

Hello World!

Failed to load lib

libopenzwave.so.1.4: cannot open shared object file: No such file or directory

One quick sym link later and it all works :)

Upvotes: 0

Seididieci
Seididieci

Reputation: 404

Have you tried to use your lib inside a C program? Maybe that OpenZWave has some dependencies that are missing on linux...

Upvotes: 2

Related Questions