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

Failed to build C code by using generated NDK toolchain


我正在为 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

谢谢,


环境:

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

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

NDK r13(未发布)也将附带一个cmake工具链文件,用于直接使用cmake(方法见)。