Cross compiling for armv7-linux-androideabi failing with linker error


#1

I have the following in my .cargo/config:

[target.armv7-linux-androideabi]
linker = "/Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld"

When I try to cross-compile for Android with cargo build --no-default-features --target armv7-linux-androideabi I get the following error:

 /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld: -Wl,--as-needed: unknown option
           /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld: use the --help option for usage information

The full output is the following:

error: linking with `/Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld` failed: exit code: 1
  |
  = note: "/Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/minigame-8aa50d3a9c6429d3.0.o" "-o" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libminigame-8aa50d3a9c6429d3.so" "-Wl,--version-script=/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/rustc.gg2YR6zfP0OC/list" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps" "-L" "/Users/tanis/Documents/minigame-rust/target/debug/deps" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libsdl2-7568fbe83eca27d0.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/liblazy_static-ad1e8eccb670bee7.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/librand-c9d9fbdab2355ee4.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum-1dc0cc712369fa49.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_iter-e3d53b72a44dd492.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_integer-f095de9fdbda10f9.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libbitflags-65ddff5d2b91509e.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_traits-69f4129d46397118.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libsdl2_sys-e54b48e6ddb030cc.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/liblibc-d69a024cc1f6bd5d.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libstd-f4594d3e53dcb114.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libpanic_unwind-a0157c0ca919c364.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libunwind-488b4ab4bd53a138.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/librand-1efbcfd8938372b6.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcollections-532a3dbf317eff86.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liballoc-ca07b617414dd0fa.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liballoc_system-68e33a366943aef4.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liblibc-88c194c15fdb6521.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libstd_unicode-cfbd6648f7db2ee5.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcore-687e6a964d22cbb4.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcompiler_builtins-987729be881d4d32.rlib" "-l" "SDL2" "-l" "dl" "-l" "log" "-l" "gcc" "-l" "gcc" "-l" "c" "-l" "m" "-shared"
  = note: /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld: -Wl,--as-needed: unknown option
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld: use the --help option for usage information

I double checked and the linker does not support the -Wl flag. Is there any way to remove it from the command being generated by cargo?


#2

Hi Tanis, that target expects to use gcc as the linker not ld, so you need to change linker to point to gcc instead.


#3

I tried that before switching to ld but it threw a different error that I thought was caused because I was calling the compiler instead of the linker tbh.
I’m not at the PC at the moment. I’ll post the other error as soon as I can. Cheers!


#4

Here’s the full error with gcc instead of ld

error: linking with `/Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc` failed: exit code: 1
  |
  = note: "/Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/minigame-8aa50d3a9c6429d3.0.o" "-o" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libminigame-8aa50d3a9c6429d3.so" "-Wl,--version-script=/var/folders/19/jchkphxs3095s37rl_p0t2dw0000gn/T/rustc.vVFEQ26dTDAP/list" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps" "-L" "/Users/tanis/Documents/minigame-rust/target/debug/deps" "-L" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libsdl2-7568fbe83eca27d0.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/liblazy_static-ad1e8eccb670bee7.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/librand-c9d9fbdab2355ee4.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum-1dc0cc712369fa49.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_iter-e3d53b72a44dd492.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_integer-f095de9fdbda10f9.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libbitflags-65ddff5d2b91509e.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libnum_traits-69f4129d46397118.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/libsdl2_sys-e54b48e6ddb030cc.rlib" "/Users/tanis/Documents/minigame-rust/target/armv7-linux-androideabi/debug/deps/liblibc-d69a024cc1f6bd5d.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libstd-f4594d3e53dcb114.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libpanic_unwind-a0157c0ca919c364.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libunwind-488b4ab4bd53a138.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/librand-1efbcfd8938372b6.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcollections-532a3dbf317eff86.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liballoc-ca07b617414dd0fa.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liballoc_system-68e33a366943aef4.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/liblibc-88c194c15fdb6521.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libstd_unicode-cfbd6648f7db2ee5.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcore-687e6a964d22cbb4.rlib" "/Users/tanis/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/armv7-linux-androideabi/lib/libcompiler_builtins-987729be881d4d32.rlib" "-l" "SDL2" "-l" "dl" "-l" "log" "-l" "gcc" "-l" "gcc" "-l" "c" "-l" "m" "-shared"
  = note: /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_so.o: No such file or directory
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_so.o: No such file or directory
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lSDL2
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -llog
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
          /Users/tanis/Documents/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lm
          collect2: error: ld returned 1 exit status

This makes me wonder if I could need a standalone Android toolchain instead of calling directly into the shared toolchain. (the missing crtbegin_so.o leads me to believe that)
But on the other hand it’s also looking for SDL2 which is clearly not going to be available as I am going to compile that in a different Android gradle-based project.

I could really use a hand to understand how the cargo build toolchain works.


#5

Looks like you probably need -C link-arg=--sysroot=$NDK_ROOT/platforms/android-<api level you are targeting>/arch-arm. You will also have to build SDL first and pass the location of that to the linker too.


#6

For the time being I solved the first part by creating the standalone toolchain for my targets so that I don’t need to set the sysroot, but your suggestion is a better approach as I won’t need to create all the standalone toolchains. I’ll try that later.

SDL is a bit more complicated because my gradle project isn’t just building the library. It’s also building the app that will run that library and my Rust code so I was hoping to do it like I did with Xcode and have the Android project be the one that runs the whole show.

It looks like I’ll have to split it up into two targets and make the gradle first generate the SDL library, then call out to cargo to build the Rust library and then build the actual Android app. I’ll have to have a look at how to do this correctly.

On top of that, I believe that I need to tell cargo where the SDL library lives (and actually my SDL isn’t called libSDL.so so I might need to set the correct name, is that doable?), how would you do that?

Cheers!