CalumMcCall
CalumMcCall

Reputation: 1687

Linking fails using x86_64 NDK toolchain on x86_64

I'm building a library for 32/64-bit ARM and x86, using the Android NDK toolchain. The library is written in Rust, and I'm using clang as the linker. I'm using the Android NDK default toolchains to get my Makefile working. Compiling on all platforms works fine, except for x86_64. I'm compiling on x86_64 Linux, but I get the same issue on Mac OSX. The error I get is the following:

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)

The relevant section of the Makefile is:

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"

The rest of the Makefile can be viewed here. How come only x86_64 has a problem with sourcing pthread and other libraries? Is the assumption that you would use 'native' compiling for the host platform, instead of trying to use the NDK to 'cross-compile'?

Also, where does the "--as-needed" flag come from? I don't specify it in the link_args in the Makefile. I'm quite new to Rust and cross-compiling with Android, so I'm not even sure whether it's rust, clang or the NDK that is generating this flag. Some advice about this would also be useful, as this flag is causing the build to fail on MacOS, where '--as-needed' isn't supported.

Upvotes: 0

Views: 1811

Answers (1)

CalumMcCall
CalumMcCall

Reputation: 1687

This issue was caused by me using the wrong target when invoking Rust. I was using x86_64-unknown-linux-gnu because I thought there was no target for x86_64 Android. However, there is indeed x86_64-linux-android, but you have to have an up-to-date rust compiler version selected as the default in order to see this target when searching. It doesn't exist for older rust compiler versions such as 1.16.0. With the right target selected, the compilation works.

Upvotes: 0

Related Questions