如何使用ndk-build.cmd构建Android.so文件

How to build the Android .so file using ndk-build.cmd

本文关键字:Android so 文件 构建 cmd 何使用 ndk-build      更新时间:2023-10-16

在尝试为Unity构建本机c++插件时获取DllNotFoundException。

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();
};

然后我通过这个命令生成一个so文件

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

然后我在Assets/Plugins/x86_64文件夹中添加了这个.so文件,在我的unity c#代码中,我正在尝试运行这段代码。

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

在收到这个错误后,我尝试将so文件移动到不同的位置并进行测试。我总是收到DllNotFoundException。

Found that the DllNotFoundException was coming bcoz the .so file 
is not build using ndk-build.cmd. 
Plz help me with the steps to build the .so file for android.

NDK文档包含有关如何为Android构建库的说明。安卓工作室并不是严格要求的,但它使它更容易。如果你不使用它,你必须手动安装NDK。