无法使用生成的NDK工具链构建C代码

时间:2016-09-08 07:04:32

标签: c android-ndk cmake cross-compiling


我正在为android 5.1.1构建一个c库。 首先,我按照 google ndk standalone toolchain guild建立一个独立的工具链。

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

之后,我在〜/ .bashrc

中导出CC,AR和RANLIB
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"

但是当我尝试使用cmake ..进行编译时。我有以下错误:

-- 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
  --verbose=1

  /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".

似乎编译器无法找到“crtbegin_dynamic.o”,“crtend_android.o”,“libc”也不能找到“libdl”。但是当我检查时,这些文件都在$ HOME / utilities / ndk / sysroot / usr / lib文件夹中。我已经在CC,AR和RANLIB中设置了--sysroot。

更新1:
根据Dan Albert的评论更改了工具链生成命令后,命令cmake ..能够成功运行。但是当我运行make .时出现以下错误:

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

谢谢,

环境:

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

2 个答案:

答案 0 :(得分:1)

如果您想使用cmake,则不需要独立的工具链。 Android Studio现在支持cmake:http://tools.android.com/tech-docs/external-c-builds

NDK r13(尚未发布)还将发送一个cmake工具链文件,以便直接使用cmake(https://stackoverflow.com/a/5099229/632035中显示的方法)。

答案 1 :(得分:0)

您不应该明确添加--sysroot=。独立工具链已经知道正确的工具链。