使用生成的NDK工具链构建C代码失败

时间:2021-10-07 12:17:35


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.

我正在为android 5.1.1构建一个c库。首先,我通过遵循谷歌ndk独立工具链公会来构建一个独立的工具链。

<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

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

之后,我在~/.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"

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

但是当我尝试用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".

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.

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


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:

更新1:在根据Dan Albert的评论修改了工具链生成命令之后,命令cmake。能够顺利运行。但是当我跑的时候。我得到以下错误:

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
  • 操作系统:Ubuntu 16.04 LTS
  • gcc / g++: 4.9.3
  • gcc / g + +:4.9.3
  • AOSP: 5.1.1_r30
  • AOSP:5.1.1_r30
  • NDK: android-ndk-r12b
  • NDK:android-ndk-r12b
  • cmake: 3.5.1
  • cmake:3.5.1

2 个解决方案

#1


1  

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

如果希望使用cmake,则不需要单独的工具链。Android Studio原生支持cmake now: http://tools.android.com/tech-docs/external-c-build

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

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

#2


0  

You shouldn't add --sysroot= explicitly. Standalone toolchain already knows about proper one.

不应该显式地添加-sysroot=。独立的工具链已经知道正确的一个。

#1


1  

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

如果希望使用cmake,则不需要单独的工具链。Android Studio原生支持cmake now: http://tools.android.com/tech-docs/external-c-build

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

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

#2


0  

You shouldn't add --sysroot= explicitly. Standalone toolchain already knows about proper one.

不应该显式地添加-sysroot=。独立的工具链已经知道正确的一个。