Swift for Android: \`ld\` cannot find \`-lgcc\` in swift for android compilation linking step

Issue

Moving from Unix Stack Exchange to here, my bounty there failed:

I’m trying to compile Swift code on Ubuntu 16.04 LTS, following these instructions fairly closely. I was able to download the NDK r14 okay, download the libicu tools and build them, and download the swift source and build that against the libicu tools. I get all the way down to compiling actual swift code and get this:

#build.sh 
#(I symlinked the androideabi ld.gold to /usr/bin/ld.armv7, based on a swift bug ticket's advice. Thats the only thing different from the instructions)

/home/mike/workspace/swift-source/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc \
-use-ldarmv7 \
-tools-directory /home/mike/workspace/android-ndk-r14/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/arm-linux-androideabi/bin \
-target armv7-none-linux-androideabi \
-sdk /home/mike/workspace/android-ndk-r14/platforms/android-21/arch-arm \
-L /home/mike/workspace/android-ndk-r14/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \
-L /home/mike/workspace/android-ndk-r14/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9 \
hello.swift


#output
mike@mike-VirtualBox:~/workspace/HelloSwift$ ./build.sh 
/usr/bin/ld.armv7: error: cannot find -lgcc
/usr/bin/ld.armv7: error: cannot find -lgcc
/usr/bin/ld.armv7: error: cannot find -lgcc
/usr/bin/ld.armv7: error: cannot find -lgcc

I don’t have a ton of experience being this far down the toolchain, but I definitely have gcc installed, and gcc-multilib, and LD_LIBRARY_PATH set to help ld find gcc, and I’ve rebuild the ld cache with ldconfig.

When I run sudo ldconfig -p | grep gcc, I get

libgccpp.so.1 (libc6,x86-64) > /usr/lib/x86_64-linux-gnu/libgccpp.so.1
libgcc_s.so.1 (libc6,x32) > /usr/libx32/libgcc_s.so.1 
libgcc_s.so.1 (libc6,x86-64) > /lib/x86_64-linux-gnu/libgcc_s.so.1
libgcc_s.so.1 (libc6) > /usr/lib32/libgcc_s.so.1

and a few other things named libuno_<something with gcc in it>

Solution

In the newest version of android-ndk-r14b, the location of required gcc library is at:

/path/to/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x

The Swift Android docs seem to be outdated, and point to:

/path/to/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.

Making sure you link against this correct path in your call to swiftc should solve the problem.

(I’ve submitted this pull request to ensure nobody else runs into this as well.)

Answered By – Qasim

Leave a Comment