1

Getting DllNotFoundException when trying to build a native c++ plugin for Unity.

FirstDll.cpp

#include "FirstDll.h"                                                                                                                                                                

  DLLExport int add(int a, int b){
      return a+b;
  }

  FirstDll::FirstDll(){
  }

   FirstDll::~FirstDll(){
 }

FirstDll.h

#define DLLExport __declspec(dllexport)                                                                                                                                              

   extern "C"{
       DLLExport int add(int a, int b);
   }

  class FirstDll{
  public:
       FirstDll();
      ~FirstDll();
 };

I am then generating a so file via this command

g++ -dynamiclib -flat_namespace -fms-extensions FirstDll.cpp -o libmyclass.so

I am then added this .so file in Assets/Plugins/x86_64 folder and in my unity c# code, I am trying to run this piece of code.

[DllImport("myclass")]
    static extern int add(int a, int b);

After getting this error, I have tried to moved the so file to different locations and test. I am always getting DllNotFoundException.

1 Answer 1

1

Try using the following clang command instead

clang *.cpp -O3 -dynamiclib -arch i386 -arch x86_64 -o libmyclass.bundle

Then make sure to select the correct platform in Unity platform selection

Or you can check this simple plugin from Unity: SimplestPluginExample

Sign up to request clarification or add additional context in comments.

4 Comments

The plugin works fine in Unity Editor. But when I build Android apk and try to run it, then getting this error ``` 02-21 11:43:00.454 6297 6310 E Unity : Unable to find ASimplePlugin 02-21 11:43:00.803 6297 6310 E Unity : DllNotFoundException: ASimplePlugin 02-21 11:43:00.803 6297 6310 E Unity : at (wrapper managed-to-native) PluginImport.PrintANumber() 02-21 11:43:00.803 6297 6310 E Unity : at PluginImport.Start () [0x00000] in <fb0372b6d3eb4a80849b26b2b5598526>:0 ```
You need to build the native plugins for each platform before trying to run. For Android, you need the NDK setup.
I have installed NDK in unity for Android platform and my .so file is working fine now in Unity Editor, but when running in Android device, still getting the same error
Did you used the ndk-build.cmd to build the Android .so file? The native plugin needs to be built specifically for android. You can check this tutorial: youtube.com/watch?v=EElBAGkjPt4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.