问题描述
我正在尝试将 Rust 代码添加到 android NDK 示例(本机活动);每当我将 Rust 代码(编译为 .a)链接到 .so 时,它都无法运行.
I'm trying to add Rust code to an android NDK sample (native-activity);Whenever I link Rust code (compiled as a .a) into the .so , it fails to run.
我继续从这里获取信息以获得 android 感知 Rust 编译器和独立工具链"https://github.com/mozilla/rust/wiki/Doc-building-for-android
I went on information from here to get an android aware rust compiler and 'standalone toolchain'https://github.com/mozilla/rust/wiki/Doc-building-for-android
有人在 Rust IRC 频道上建议我需要在某处提及拇指";是否有传递给 rustc 的选项,或者我是否必须首先以不同的方式构建它?
Someone was suggesting on the Rust IRC channel that I need to mention 'thumb' somewhere;is there an option to pass to rustc, or did I have to build it all differently in the first place?
我当然可以调用 rust &c 在桌面版本上彼此之间.
I've certainly been able to call rust & c from eachother on desktop builds.
是否有一个示例,其中有人在 .apk 中使用了 Rust 代码;它必须很简单......很难破译复杂项目的makefile.(我认为通过阅读伺服源很难弄清楚这一点)
Is there a sample where someone has rust code working in a .apk; it would have to be simple.. its very hard to decipher makefiles for complex projects. (I think it would be very hard to figure this out from reading Servo source)
我已经验证了这个过程生成了一个可用的包没有添加 rust;如果我链接未剥离的生锈代码,它只会无法运行 - 即使没有达到(即使链接没有告诉我任何错误).如果我删除对rusty_android()"的调用,它会起作用.如果我将对 rusty_android 的调用移动到未到达的点,它仍然不起作用.我可以用 'nm' 验证 'rusty_android' 在 .so 中......未损坏.
I've verified this process generates a useable package without adding rust; it only fails to run if I link unstripped rust code - even if it isn't reached (even though the linking doesn't tell me anything is wrong). If i remove the call to 'rusty_android()', it works. If i move the call to rusty_android to a point that isn't reached, it still doesn't work. I can verify with 'nm' that 'rusty_android' is in the .so ... unmangled.
这是我当前的构建过程:它取自 android native-activity 示例;我添加了一个带有单个 extern functin 返回值的 rust 源文件,并尝试从示例的 C main 中调试打印
This is my current build process: its taken from the android native-activity sample; i've added a rust source file with a single extern functin returning a value, and tried to debug print that from the samples' C main
ANDROID_CXX = /opt/ndk_standalone/bin/arm-linux-androideabi-gcc
android:
$(ANDROID_CXX) -I/home/ME/android-ndk-r9b/sources/android/native_app_glue -c jni/main.c -o obj/local/armeabi/objs/native-activity/main.o
rustc --target=arm-linux-androideabi hello_android.rs -C android-cross-path=/opt/ndk_standalone --crate-type=staticlib -o rusty_android.a
$(ANDROID_CXX) -shared -o libs/armeabi/libnative-activity.so obj/local/armeabi/objs/native-activity/main.o obj/local/armeabi/libandroid_native_app_glue.a another.o rusty_android.a -llog -landroid -lEGL -lGLESv1_CM
ant debug
adb install -r bin/NativeActivity-debug.apk
adb shell am start -n com.example.native_activity/android.app.NativeActivity
hello_android.rs:-
use std::libc;
#[no_mangle]
pub extern fn rusty_android()->libc::c_int {
99 as libc::c_int
}
in main.c, inserted in android_main(..):-
{ extern int rusty_android(); LOGI("****rust says %d****", rusty_android()); }
通过比较 LLVM IR 和 asm 源,怀疑可能需要特定的 'thumb' 内容,但似乎奇怪的是,这将是 'arm-linux-androideabi' 之上所需的附加选项
The suspicion that something to specificy 'thumb' might be needed arose from comparing LLVM IR and asm sources, but it seems strange that it would be an additional option required over and above 'arm-linux-androideabi'
推荐答案
自从我发布这个,有人帮了我,我得到了这个有效的 makefile..
Since I posted this, someone helped out and i've got this makefile that works..
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := rust-prebuilt
LOCAL_SRC_FILES := librusty_android.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := native-activity
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES += android_native_app_glue rust-prebuilt
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
因此,使用它,您可以在 ndk 示例自己的链接过程中链接一个预构建的 rust 静态库.
So using this, one can link a prebuilt rust static library in the ndk samples' own link process.
那么它在幕后做了什么,librusty_android-> rust-prebuilt .. 它是否只是我错过了不同的链接器选项?它是以某种方式转换 libray 还是对其进行签名?
So what is it doing behind the scenes, librusty_android-> rust-prebuilt .. does it just have different linker options I missed ? is it converting the libray somehow, or signing it ?
我显然可以制作一个单独的 makefile 来编译我的 Rust 代码,然后将其称为最后一步,但简化它并找出为什么它会起作用仍然很好.
I can obviously make a single makefile to compile my rust code then call this as a final step but it would still be nice to simplify this out and find out why it works.
这篇关于如何将 Rust 代码编译并链接到 android apk 打包应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!