在 x86_64 上使用 x86_64 NDK 工具链链接失败

Linking fails using x86_64 NDK toolchain on x86_64

我正在使用 Android NDK 工具链为 32/64 位 ARM 和 x86 构建库。该库是用 Rust 编写的,我使用 clang 作为链接器。我正在使用 Android NDK default toolchains 让我的 Makefile 工作。除了 x86_64,在所有平台上编译都可以正常工作。我在 x86_64 Linux 上编译,但在 Mac OSX 上遇到同样的问题。我得到的错误如下:

error: linking with `x86_64-linux-android21-clang` failed: exit code: 1
  |
  = note: "x86_64-linux-android21-clang" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/cryptobox-f7e54e68993dbdc8.0.o" "-o" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libcryptobox-f7e54e68993dbdc8.so" "-Wl,--version-script=/tmp/rustc.QEOguYgk4mJf/list" "-Wl,--gc-sections" "-Wl,-O1" "-nodefaultlibs" "-L" "../../libsodium-android-x86_64/lib" "-L" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps" "-L" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/release/deps" "-L" "\"./libs\"" "-L" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "-l" "sodium" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libcryptobox-9b6f2a0a353ba6df.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libproteus-40e6a130b38468f5.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libcbor-024d9926f0739f39.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libbyteorder-30acb91327072c30.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libhkdf-971b9da845836207.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/libsodiumoxide-eccd1814def93cba.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/liblibsodium_sys-f96178cc67fea277.rlib" "/home/calum/code/cryptobox-jni/android/build/src/cryptobox-v1.1.1/target/x86_64-unknown-linux-gnu/release/deps/liblibc-e8bd7a8d60e9ed01.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-13f36e2630c2d79b.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-3b9d178f1de89528.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-93bb403c9fc56f72.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-a2ef7979b4b3e1d5.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-d22754c8c52de3a1.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-c53f99154bf815c4.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_unicode-1cc5fcd37568ebc4.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_system-17a71bb92a82956c.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-739908a2e215dd88.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-3f4289353c600297.rlib" "/home/calum/.rustup/toolchains/1.16.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-07bfb3bcb2a51da0.rlib" "-l" "sodium" "-l" "util" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util" "-shared" "-Wl,-soname,libcryptobox.so"
  = note: /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lutil
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lrt
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lpthread
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lgcc_s
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lpthread
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lrt
          /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lutil
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

Makefile 的相关部分是:

jni/x86_64/libcryptobox.so: libsodium | build/src/$(CRYPTOBOX_NAME)
cd build/src/$(CRYPTOBOX_NAME) && \
export PATH="${PATH}:${TOOLCHAIN}/bin" && \                                                             
cargo rustc --lib --release --target=x86_64-unknown-linux-gnu -- \
  -L ../../libsodium-android-x86_64/lib \
  -l sodium \
  -C ar=x86_64-linux-android-ar \
  -C linker=x86_64-linux-android21-clang \
  -C link_args="-Wl,-soname,libcryptobox.so"

可以查看 Makefile 的其余部分here。为什么只有 x86_64 对采购 pthread 和其他库有问题?假设您将使用 'native' 为主机平台编译,而不是尝试使用 NDK 来 'cross-compile'?

此外,“--as-needed”标志从何而来?我没有在 Makefile 的 link_args 中指定它。我对 Rust 和使用 Android 进行交叉编译还很陌生,所以我什至不确定生成此标志的是 rust、clang 还是 NDK。关于此的一些建议也很有用,因为此标志导致构建在 MacOS 上失败,其中不支持“--as-needed”。

这个问题是我在调用Rust时使用了错误的目标导致的。我使用 x86_64-unknown-linux-gnu 是因为我认为 x86_64 Android 没有目标。然而,确实有 x86_64-linux-android,但你必须选择一个最新的 rust 编译器版本作为默认版本,以便在搜索时看到这个目标。对于旧的 rust 编译器版本(例如 1.16.0),它不存在。选择正确的目标后,编译工作。