Building a toolchain with cmake to cross-compile for android

ant2009 picture ant2009 · Oct 13, 2013 · Viewed 28.3k times · Source
gcc (GCC) 4.8.1 
android-ndk-r9 

Hello,

My host machine is Fedora 19 and I want to create a tool-chain for compiling programs to run on android, later I want to extend this for iOS.

I get the following error:

Check for working C compiler: /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -- broken

I am not sure why I am getting this error, as everything has been installed. I have also installed binutils-arm-linux-gnu. However, this is my first time do this type of thing, so many I have got something mixed up.

I am trying to create a toolchain file using cmake to croos-compile to run libraries on an android device.

I have installed the android-ndk-r9 in the following location with the path to the compiler:

/opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin

arm-linux-androideabi-addr2line
arm-linux-androideabi-ar
arm-linux-androideabi-as
arm-linux-androideabi-c++
arm-linux-androideabi-c++filt
arm-linux-androideabi-cpp
arm-linux-androideabi-elfedit
arm-linux-androideabi-g++
arm-linux-androideabi-gcc
arm-linux-androideabi-gcc-4.8
arm-linux-androideabi-gcc-ar
arm-linux-androideabi-gcc-nm
arm-linux-androideabi-gcc-ranlib
arm-linux-androideabi-gcov
arm-linux-androideabi-gdb
arm-linux-androideabi-gprof
arm-linux-androideabi-ld
arm-linux-androideabi-ld.bfd
arm-linux-androideabi-ld.gold
arm-linux-androideabi-ld.mcld
arm-linux-androideabi-nm
arm-linux-androideabi-objcopy
arm-linux-androideabi-objdump
arm-linux-androideabi-ranlib
arm-linux-androideabi-readelf
arm-linux-androideabi-run
arm-linux-androideabi-size
arm-linux-androideabi-strings
arm-linux-androideabi-strip

My cross-compile file is:

include(CMakeForceCompiler)

set(toolchain_path /opt/ndk/toolchains)

# Target system
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSTEM_VERSION 1)

# Compiler to build for the target
set(CMAKE_C_COMPILER /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc)

set(CMAKE_FIND_ROOT_PATH /opt/ndk/toolchains)

I run this from my build/debug directory with my toolchain being in the root directory.

[ant@localhost debug]$ cmake -DCMAKE_TOOLCHAIN_FILE=arm-eabi-gcc.cmake ../..

Output

-- The C compiler identification is GNU 4.8.0
-- Check for working C compiler: /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
-- Check for working C compiler: /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -- broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler
  "/opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc"
  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: /home/steve/mobile_progs/linux_pjsip/build/debug/CMakeFiles/CMakeTmp

  Run Build Command:/usr/bin/gmake "cmTryCompileExec379796592/fast"

  /usr/bin/gmake -f CMakeFiles/cmTryCompileExec379796592.dir/build.make
  CMakeFiles/cmTryCompileExec379796592.dir/build

  gmake[1]: Entering directory
  `/home/steve/mobile_progs/linux_pjsip/build/debug/CMakeFiles/CMakeTmp'

  /usr/bin/cmake -E cmake_progress_report
  /home/steve/mobile_progs/linux_pjsip/build/debug/CMakeFiles/CMakeTmp/CMakeFiles
  1

  Building C object
  CMakeFiles/cmTryCompileExec379796592.dir/testCCompiler.c.o

  /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
  -o CMakeFiles/cmTryCompileExec379796592.dir/testCCompiler.c.o -c
  /home/steve/mobile_progs/linux_pjsip/build/debug/CMakeFiles/CMakeTmp/testCCompiler.c

  Linking C executable cmTryCompileExec379796592

  /usr/bin/cmake -E cmake_link_script
  CMakeFiles/cmTryCompileExec379796592.dir/link.txt --verbose=1

  /opt/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
  CMakeFiles/cmTryCompileExec379796592.dir/testCCompiler.c.o -o
  cmTryCompileExec379796592 -rdynamic 

  /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtbegin_dynamic.o: No such file or directory


  /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtend_android.o: No such file or directory

  /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -lc

  /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -ldl

  collect2: error: ld returned 1 exit status

  gmake[1]: *** [cmTryCompileExec379796592] Error 1

  gmake[1]: Leaving directory
  `/home/steve/mobile_progs/linux_pjsip/build/debug/CMakeFiles/CMakeTmp'

  gmake: *** [cmTryCompileExec379796592/fast] Error 2

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:4 (project)

Many thanks in advance,

Answer

ant2009 picture ant2009 · Oct 15, 2013

I managed to solve the problem by first going to this website:

http://developer.android.com/tools/sdk/ndk/index.html

There is an example of using the standalone toolchain that comes with NDK.

make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.8

Extracted the into my /opt directory.

And using this sample toolchain cmake file

# Target system
set(CMAKE_SYSTEM_NAME  Android)

set(CMAKE_SYSTEM_VERSION 1)

# Compiler to build for the target
set(CMAKE_C_COMPILER /opt/arm-linux-androideabi-4.8/bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER /opt/arm-linux-androideabi-4.8/bin/arm-linux-androideabi-g++)

Everything just worked after that. However, I couldn't get my previous problem working. Maybe I have set some environmental variables incorrectly to the wrong paths.

Hope this helps someone else.