Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a toolchain with cmake to cross-compile for android

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,

like image 946
ant2009 Avatar asked Oct 13 '13 09:10

ant2009


People also ask

Can CMake cross compile?

Cross-compiling is fully supported by CMake, ranging from cross-compiling from Linux to Windows; cross-compiling for supercomputers, through to cross-compiling for small embedded devices without an operating system (OS).

Is CMake a toolchain?

Introduction. CMake uses a toolchain of utilities to compile, link libraries and create archives, and other tasks to drive the build. The toolchain utilities available are determined by the languages enabled.

What is CMake used for in Android Studio?

The Android NDK supports using CMake to compile C and C++ code for your application.

What is Cmake_sysroot?

The CMAKE_SYSROOT content is passed to the compiler in the --sysroot flag, if supported. The path is also stripped from the RPATH / RUNPATH if necessary on installation. The CMAKE_SYSROOT is also used to prefix paths searched by the find_* commands.


3 Answers

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.

like image 130
ant2009 Avatar answered Oct 12 '22 11:10

ant2009


Why you don't try this android-cmake. I still use this script and it works fairly well. If that approach does not fit your needs, you could use it as an inspiration anyway :-) .

like image 44
javier-sanz Avatar answered Oct 12 '22 11:10

javier-sanz


For original problem from ant2009, please have a try to add following lines in .cmake:

SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=/opt/ndk/platforms/android-23/arch-arm" CACHE STRING "" FORCE)

SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=/opt/ndk/platforms/android-23/arch-arm" CACHE STRING "" FORCE)
like image 1
zyzytoby Avatar answered Oct 12 '22 10:10

zyzytoby