Failed to cross-compile library from Windows to Android

爷,独闯天下 提交于 2021-02-11 07:09:22

问题


I'm trying to cross-compile a Rust library to 4 Android targets (i386, x86_64, armv7, and aarch64) from Windows.

I have set up Android NDK toolchain for these targets and tried to compile my library with help of this tutorial.

I think I've set up the toolchain correctly, and I'm able to successfully execute aarch64-linux-android-clang in the terminal.

Build log:

cargo build --target aarch64-linux-android --release --verbose
       Fresh cc v1.0.28
       Fresh autocfg v0.1.1
       Fresh version_check v0.1.5
       Fresh void v1.0.2
       Fresh cfg-if v0.1.6
       Fresh rustc-demangle v0.1.13
       Fresh byteorder v1.2.7
       Fresh either v1.5.0
       Fresh ascii v0.9.1
       Fresh jni-sys v0.3.0
       Fresh cesu8 v1.1.0
       Fresh unreachable v1.0.0
       Fresh log v0.4.6
       Fresh winapi v0.3.6
   Compiling backtrace-sys v0.1.28
       Fresh libc v0.2.46
     Running `C:\Users\My Username\CLionProjects\my_project\target\release\build\backtrace-sys-3bfde344967f80f2\build-script-build`
       Fresh winapi-util v0.1.1
       Fresh memchr v2.1.2
       Fresh same-file v1.0.4
       Fresh combine v3.6.6
       Fresh walkdir v2.2.7
error: failed to run custom build command for `backtrace-sys v0.1.28`
process didn't exit successfully: `C:\Users\My Username\CLionProjects\my_project\target\release\build\backtrace-sys-3bfde344967f80f2\build-script-build` (exit code: 101)
--- stdout
cargo:rustc-cfg=rbt
TARGET = Some("aarch64-linux-android")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-pc-windows-msvc")
CC_aarch64-linux-android = None
CC_aarch64_linux_android = None
TARGET_CC = None
CC = None
CFLAGS_aarch64-linux-android = None
CFLAGS_aarch64_linux_android = None
TARGET_CFLAGS = None
CFLAGS = None
running: "aarch64-linux-android-clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=aarch64-linux-android" "-I" "src/libbacktrace" "-I" "C:\\Users\\My Username\\CLionProjects\\my_project\\target\\aarch64-linux-android\\release\\build\\backtrace-sys-5aff8725b1b77839\\out" "-fvisibility=hidden" "-DBACKTRACE_ELF_SIZE=64" "-DBACKTRACE_SUPPORTED=1" "-DBACKTRACE_USES_MALLOC=1" "-DBACKTRACE_SUPPORTS_THREADS=0" "-DBACKTRACE_SUPPORTS_DATA=0" "-D_GNU_SOURCE=1" "-D_LARGE_FILES=1" "-Dbacktrace_full=__rbt_backtrace_full" "-Dbacktrace_dwarf_add=__rbt_backtrace_dwarf_add" "-Dbacktrace_initialize=__rbt_backtrace_initialize" "-Dbacktrace_pcinfo=__rbt_backtrace_pcinfo" "-Dbacktrace_syminfo=__rbt_backtrace_syminfo" "-Dbacktrace_get_view=__rbt_backtrace_get_view" "-Dbacktrace_release_view=__rbt_backtrace_release_view" "-Dbacktrace_alloc=__rbt_backtrace_alloc" "-Dbacktrace_free=__rbt_backtrace_free" "-Dbacktrace_vector_finish=__rbt_backtrace_vector_finish" "-Dbacktrace_vector_grow=__rbt_backtrace_vector_grow" "-Dbacktrace_vector_release=__rbt_backtrace_vector_release" "-Dbacktrace_close=__rbt_backtrace_close" "-Dbacktrace_open=__rbt_backtrace_open" "-Dbacktrace_print=__rbt_backtrace_print" "-Dbacktrace_simple=__rbt_backtrace_simple" "-Dbacktrace_qsort=__rbt_backtrace_qsort" "-Dbacktrace_create_state=__rbt_backtrace_create_state" "-Dbacktrace_uncompress_zdebug=__rbt_backtrace_uncompress_zdebug" "-o" "C:\\Users\\My Username\\CLionProjects\\my_project\\target\\aarch64-linux-android\\release\\build\\backtrace-sys-5aff8725b1b77839\\out\\src/libbacktrace\\alloc.o" "-c" "src/libbacktrace/alloc.c"

--- stderr
thread 'main' panicked at '

Internal error occurred: Failed to find tool. Is `aarch64-linux-android-clang` installed? (see https://github.com/alexcrichton/cc-rs#compile-time-requirements for help)

', C:\Users\My Username\.cargo\registry\src\github.com-1ecc6299db9ec823\cc-1.0.28\src\lib.rs:2314:5
stack backtrace:
   0: std::sys::windows::backtrace::set_frames
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\sys\windows\backtrace\mod.rs:104
   1: std::sys::windows::backtrace::set_frames
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\sys\windows\backtrace\mod.rs:104
   2: std::sys_common::backtrace::_print
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\sys_common\backtrace.rs:71
   3: std::sys_common::backtrace::_print
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\sys_common\backtrace.rs:71
   4: std::panicking::default_hook::{{closure}}
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:211
   5: std::panicking::default_hook
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:227
   6: std::panicking::rust_panic_with_hook
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:476
   7: std::panicking::continue_panic_fmt
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:390
   8: std::panicking::begin_panic_fmt
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:345
   9: cc::Tool::to_command
  10: cc::Build::compile
  11: core::ptr::drop_in_place
  12: std::fs::File::create
  13: std::rt::lang_start_internal::{{closure}}
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\rt.rs:59
  14: std::rt::lang_start_internal::{{closure}}
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\rt.rs:59
  15: panic_unwind::__rust_maybe_catch_panic
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libpanic_unwind\lib.rs:102
  16: std::panicking::try
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:289
  17: std::panicking::try
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:289
  18: std::panicking::try
             at /rustc/b6c32da9b0481e3e9d737153286b3ff8aa39a22c\src/libstd\panicking.rs:289
  19: main
  20: invoke_main
             at d:\agent\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  21: invoke_main
             at d:\agent\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  22: BaseThreadInitThunk
  23: RtlUserThreadStart

My .cargo/config file:

[target.aarch64-linux-android]
ar = "C:/NDK/arm64/bin/aarch64-linux-android-ar"
linker = "C:/NDK/arm64/bin/aarch64-linux-android-clang"

[target.armv7-linux-androideabi]
ar = "C:/NDK/arm/bin/arm-linux-androideabi-ar"
linker = "C:/NDK/arm/bin/arm-linux-androideabi-clang"

[target.i686-linux-android]
ar = "C:/NDK/x86/bin/i686-linux-android-ar"
linker = "C:/NDK/x86/bin/i686-linux-android-clang"

[target.x86_64-linux-android]
ar = "C:/NDK/x86_64/bin/x86_64-linux-android-ar"
linker = "C:/NDK/x86_64/bin/x86_64-linux-android-clang"

回答1:


I have to use .bat instead of .exe file

[target.aarch64-linux-android]
ar = "C:/NDK/arm64/bin/aarch64-linux-android-ar"
linker = "C:/NDK/arm64/bin/aarch64-linux-android-clang.cmd"

also env variables:

SET CC=C:\NDK\arm64\bin\aarch64-linux-android-clang.cmd
SET AR=C:\NDK\arm64\bin\aarch64-linux-android-ar


来源:https://stackoverflow.com/questions/54189024/failed-to-cross-compile-library-from-windows-to-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!