我有一个共享库文件
我的库是用Rust编写的,因此我没有头文件,并且我想通过cpp文件调用其中的函数,但似乎无法正确链接该库。为了测试,我制作了一个简单的函数: Rust代码:
faceblaster-engine.so
,编译为arm-linux-androideabi
,放置在Android Studio的jniLibs文件夹中。我还有一个简单的cpp文件在jni文件夹中。我的库是用Rust编写的,因此我没有头文件,并且我想通过cpp文件调用其中的函数,但似乎无法正确链接该库。为了测试,我制作了一个简单的函数: Rust代码:
#[no_mangle]
pub extern fn rust_test() -> c_int {
82 as c_int
}
C++
extern "C" {
// Test for calling rust function
int rust_test();
jint
Java_com_fureality_faceblaster_MainActivity_testRustLaunch(JNIEnv* env, jobject thiz)
{
return rust_test();
}
} // End extern
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := faceblaster-engine
LOCAL_SRC_FILES := ../jniLibs/$(TARGET_ARCH_ABI)/libfaceblaster-engine.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := faceblaster
LOCAL_SRC_FILES := gl-tests.cpp
LOCAL_SHARED_LIBRARIES := faceblaster-engine
include $(BUILD_SHARED_LIBRARY)
错误
/home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.cpp
Error:(23) undefined reference to `rust_test'
Error:error: ld returned 1 exit status
make: *** [/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libfaceblaster.so] Error 1
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/home/nathan/Development/bin/android-ndk-r10d/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/Android.mk APP_PLATFORM=android-21 NDK_OUT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj NDK_LIBS_OUT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/lib APP_ABI=all
Error Code:
2
Output:
/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/faceblaster//home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.o: In function `Java_com_fureality_faceblaster_MainActivity_testRustLaunch':
/home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.cpp:23: undefined reference to `rust_test'
collect2: error: ld returned 1 exit status
make: *** [/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libfaceblaster.so] Error 1
我不知道我的makefile是否被识别或者它是否正确?有人知道如何正确地链接这个.so文件并注册我想使用的函数吗?
感谢提前任何帮助!
编辑-添加了Java源代码和rust代码编译方法
Java
public class MainActivity extends Activity {
// External libraries to load
static {
System.loadLibrary("faceblaster-engine");
System.loadLibrary("faceblaster");
}
// External functions to register
public native int testRustLaunch();
@Override
protected void onCreate(Bundle savedInstanceState) {
// Other stuff omitted for brevity
Log.d(TAG, "Testing call...");
int test = testRustLaunch();
Log.d(TAG, "Received: " + test);
}
}
Rust编译
cargo build --target=arm-linux-androideabi
# /project/.cargo/config file
[target.arm-linux-androideabi]
linker = "/opt/ndk_standalone/bin/arm-linux-androideabi-gcc"
# Cargo.toml
[lib]
name = "faceblaster-engine"
crate_type = ["dylib"]
编辑2
我编辑了build.gradle
脚本,现在我的Android.mk正在被读取和使用,但我仍然得到相同的编译错误:(
编辑3
结果发现下面的两个答案都有助于解决问题。主要原因是Android Studio没有正确识别我的Makefile,rust代码没有正确声明为 #[no_mangle] pub extern
,而且我的Makefile也出了很多问题。