【问题标题】:Failed to build C code by using generated NDK toolchain使用生成的 NDK 工具链构建 C 代码失败
【发布时间】:2017-01-16 00:10:50
【问题描述】:


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

【问题讨论】:

  • 您将 sysroot 设置为$HOME/utilities/ndk,因此crtbegin_dynamic.o 相对于它位于/sysroot/usr/lib。通常,编译器在“/usr/lib”下搜索,而不是在/sysroot/usr/lib
  • --toolchain=x86_64-linux-android-4.9 是错字吗?如果你想为 ARM 构建它应该是--toolchain=arm-linux-androideabi-4.9。另外,由于您使用的是r12,我应该指出该工具的新版本是make_standalone_toolchain.py,您可以使用--arch arm
  • 正如 Dan Albert 指出的,感谢您的评论 :)。 --toolchain 使用了不正确的值。在我将工具链值更改为arm-linux-androideabi-4.9 之后。配置过程能够成功完成。但是,当我执行make 命令来制作二进制文件时。我收到一个错误arm-linux-androideabi-gcc-4.9.x: error: unrecognized command line option '-m64'。不知道如何解决。

标签: c android-ndk cmake cross-compiling


【解决方案1】:

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

NDK r13(尚未发布)还将提供一个cmake工具链文件,用于直接使用cmake(方法见https://stackoverflow.com/a/5099229/632035)。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 2012-02-25
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多