Failed to build C code by using generated NDK toolchain

Issue

I am working on build a c library for android 5.1.1. First I build a standalone toolchain by following the google ndk standalone toolchain guild.

<prebuilt_ndk_r12b_path>/build/tools/make-standalone-toolchain.sh --platformandroid-22 --ndk-dir<prebuilt_ndk_r12b_path> --install-dir/home/r0ng/utilities/ndk --toolchainx86_64-linux-android-4.9

After that, I exported CC, AR and RANLIB in ~/.bashrc

export ANDROID_NDK/home/r0ng/utilities/ndk
SYSROOT$ANDROID_NDK/sysroot

export CC"$ANDROID_NDK/bin/arm-linux-androideabi-gcc-4.9.x --sysroot$SYSROOT"
export AR"$ANDROID_NDK/bin/arm-linux-androideabi-gcc-ar --sysroot$SYSROOT"
export RANLIB"$ANDROID_NDK/bin/arm-linux-androideabi-gcc-ranlib --sysroot$SYSROOT"

But when I tried to compile by using cmake ... I had the following errors:

-- The C compiler identification is GNU 4.9.0
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
-- Check for working C compiler: /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc -- broken
CMake Error at /usr/share/cmake-3.5/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler "/home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc" is
  not able to compile a simple test program.

  It fails with the following output:

   Change Dir: /home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp



  Run Build Command:"/usr/bin/make" "cmTC_8d7ca/fast"

  /usr/bin/make -f CMakeFiles/cmTC_8d7ca.dir/build.make
  CMakeFiles/cmTC_8d7ca.dir/build

  make[1]: Entering directory
  '/home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp'

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

  /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
  --sysroot/home/r0ng/utilities/ndk -o
  CMakeFiles/cmTC_8d7ca.dir/testCCompiler.c.o -c
  /home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp/testCCompiler.c

  Linking C executable cmTC_8d7ca

  /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_8d7ca.dir/link.txt
  --verbose1

  /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
  --sysroot/home/r0ng/utilities/ndk
  CMakeFiles/cmTC_8d7ca.dir/testCCompiler.c.o -o cmTC_8d7ca -rdynamic


  /home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtbegin_dynamic.o: No such file or directory


  /home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
  error: cannot open crtend_android.o: No such file or directory


  /home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -lc


  /home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
  error: cannot find -ldl

  collect2: error: ld returned 1 exit status

  CMakeFiles/cmTC_8d7ca.dir/build.make:97: recipe for target 'cmTC_8d7ca'
  failed

  make[1]: *** [cmTC_8d7ca] Error 1

  make[1]: Leaving directory
  '/home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp'

  Makefile:126: recipe for target 'cmTC_8d7ca/fast' failed

  make: *** [cmTC_8d7ca/fast] Error 2





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


-- Configuring incomplete, errors occurred!
See also "/home/r0ng/projects/relic/build/CMakeFiles/CMakeOutput.log".
See also "/home/r0ng/projects/relic/build/CMakeFiles/CMakeError.log".

It seems that the compiler is unable to find “crtbegin_dynamic.o”, “crtend_android.o”, “libc” neither “libdl”. But when I checked, those files are all in the folder $HOME/utilities/ndk/sysroot/usr/lib. And I already set the –sysroot in CC, AR and RANLIB.


Update 1 :
After changed the toolchain generation command based on Dan Albert’s comment, The command cmake .. is able to be run successfully. But when I run make . I got following error:

arm-linux-androideabi-gcc-4.9.x: error: unrecognized command line option '-m64'
src/CMakeFiles/relic.dir/build.make:62: recipe for target 'src/CMakeFiles/relic.dir/relic_err.c.o' failed
make[2]: *** [src/CMakeFiles/relic.dir/relic_err.c.o] Error 1
CMakeFiles/Makefile2:120: recipe for target 'src/CMakeFiles/relic.dir/all' failed
make[1]: *** [src/CMakeFiles/relic.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2

Thanks,


Environment:

  • OS: Ubuntu 16.04 LTS
  • gcc / g++: 4.9.3
  • AOSP: 5.1.1_r30
  • NDK: android-ndk-r12b
  • cmake: 3.5.1

Solution

If you want to use cmake, you don’t need a standalone toolchain. Android Studio natively supports cmake now: http://tools.android.com/tech-docs/external-c-builds

NDK r13 (not released yet) will also ship a cmake toolchain file for using cmake directly (the method shown in https://stackoverflow.com/a/5099229/632035).

Answered By – Dan Albert

Leave a Comment