android studio 使用cmake支持C++出现无法sync cmake error的问题

问题

错误信息

External Native Build Issues
Build command failed.
Error while executing '/root/Android/Sdk/cmake/3.6.3155560/bin/cmake' with arguments {-H/root/AndroidStudioProjects/AccessQRat/app -B/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/debug/x86_64 -GAndroid Gradle - Ninja -DANDROID_ABI=x86_64 -DANDROID_NDK=/root/Android/Sdk/ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/debug/obj/x86_64 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/root/Android/Sdk/cmake/3.6.3155560/bin/ninja -DCMAKE_TOOLCHAIN_FILE=/root/Android/Sdk/cmake/3.6.3155560/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 -DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions}
-- Check for working C compiler: /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- broken
-- Configuring incomplete, errors occurred!
See also "/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/debug/x86_64/CMakeFiles/CMakeOutput.log".
See also "/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/debug/x86_64/CMakeFiles/CMakeError.log".
CMake Error at /root/Android/Sdk/cmake/3.6.3155560/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler
  "/root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
  is not able to compile a simple test program.
  It fails with the following output:
   Change Dir: /root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/debug/x86_64/CMakeFiles/CMakeTmp
  Run Build Command:"/root/Android/Sdk/cmake/3.6.3155560/bin/ninja"
  "cmTC_a0fde"
  [1/2] Building C object CMakeFiles/cmTC_a0fde.dir/testCCompiler.c.o
  [2/2] Linking C executable cmTC_a0fde
  FAILED: : &&
  /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
  -target x86_64-none-linux-android -gcc-toolchain
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64
  --sysroot=/root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64 -g
  -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong
  -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security
  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings
  -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro
  -Wl,-z,now -Wl,--gc-sections -Wl,-z,nocopyreloc -pie -fPIE
  CMakeFiles/cmTC_a0fde.dir/testCCompiler.c.o -o cmTC_a0fde -lm && :
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/crtbegin_dynamic.o:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/crtend_android.o:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libm.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libdl.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libc.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libdl.so:
  file is empty
Error:error: linker command failed with exit code 1 (use -v to see
  invocation)
  ninja: build stopped: subcommand failed.
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt
Error:executing external native build for cmake /root/AndroidStudioProjects/AccessQRat/app/CMakeLists.txt
Build command failed.
Error while executing '/root/Android/Sdk/cmake/3.6.3155560/bin/cmake' with arguments {-H/root/AndroidStudioProjects/AccessQRat/app -B/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/release/x86_64 -GAndroid Gradle - Ninja -DANDROID_ABI=x86_64 -DANDROID_NDK=/root/Android/Sdk/ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/release/obj/x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/root/Android/Sdk/cmake/3.6.3155560/bin/ninja -DCMAKE_TOOLCHAIN_FILE=/root/Android/Sdk/cmake/3.6.3155560/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 -DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions}
-- Check for working C compiler: /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- broken
-- Configuring incomplete, errors occurred!
See also "/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/release/x86_64/CMakeFiles/CMakeOutput.log".
See also "/root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/release/x86_64/CMakeFiles/CMakeError.log".
CMake Error at /root/Android/Sdk/cmake/3.6.3155560/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler
  "/root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
  is not able to compile a simple test program.
  It fails with the following output:
   Change Dir: /root/AndroidStudioProjects/AccessQRat/app/.externalNativeBuild/cmake/release/x86_64/CMakeFiles/CMakeTmp
  Run Build Command:"/root/Android/Sdk/cmake/3.6.3155560/bin/ninja"
  "cmTC_f8bcf"
  [1/2] Building C object CMakeFiles/cmTC_f8bcf.dir/testCCompiler.c.o
  [2/2] Linking C executable cmTC_f8bcf
  FAILED: : &&
  /root/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
  -target x86_64-none-linux-android -gcc-toolchain
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64
  --sysroot=/root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64 -g
  -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong
  -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security
  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings
  -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro
  -Wl,-z,now -Wl,--gc-sections -Wl,-z,nocopyreloc -pie -fPIE
  CMakeFiles/cmTC_f8bcf.dir/testCCompiler.c.o -o cmTC_f8bcf -lm && :
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/crtbegin_dynamic.o:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/crtend_android.o:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libm.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libdl.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libc.so:
  file is empty
  /root/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld:
Error:
  /root/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64/usr/lib/../lib64/libdl.so:
  file is empty
Error:error: linker command failed with exit code 1 (use -v to see
  invocation)
  ninja: build stopped: subcommand failed.
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt
Error:executing external native build for cmake /root/AndroidStudioProjects/AccessQRat/app/CMakeLists.txt

于是android studio无法sync
(这里AccessQRat是创建的project的名称)

问题原因

我的ndk文件夹android-21下文件不全,但是android-24文件夹文件是全的,然而cmake默认选择了21,虽然我也不知道为什么就选择了21

解决方法

首先确认自己哪一个ndk版本是全的,找到ndk-build/platforms文件夹下,android-xx 后面的xx代表数字,即platmform的版本,根据我这里的提示,显然我的21版本的NDK有文件有问题,所以我选择换一个版本,因为我发现我24的版本是全的,具体的可以使用readelf命令来查看那几个文件是不是完整有效的.
找到ndk-build下cmake,进入

cd ~/Sdk/cmake/3.6.3155560/
#使用vim进行编辑,也可以使用其他进行编辑
vim android.toolchain.cmake

找到关于PLATFORM和PLATFORM_LEVEL的那几个语句,

if(ANDROID_PLATFORM MATCHES "^android-([0-8]|10|11)$")
    set(ANDROID_PLATFORM android-9)
elseif(ANDROID_PLATFORM STREQUAL android-20)
    set(ANDROID_PLATFORM android-19)
elseif(NOT ANDROID_PLATFORM)
    set(ANDROID_PLATFORM android-9)
endif()
string(REPLACE "android-" "" ANDROID_PLATFORM_LEVEL ${ANDROID_PLATFORM})
if(ANDROID_ABI MATCHES "64(-v8a)?$" AND ANDROID_PLATFORM_LEVEL LESS 21)
    set(ANDROID_PLATFORM android-21)
    set(ANDROID_PLATFORM_LEVEL 21)
endif()
if(NOT ANDROID_STL)
    set(ANDROID_STL gnustl_static)
endif()

这里可以猜到是通过set来set版本,所以我最终选择不让他自动判断,直接强行设置为24,即在这段后面添加两个set语句

if(ANDROID_PLATFORM MATCHES "^android-([0-8]|10|11)$")
    set(ANDROID_PLATFORM android-9)
elseif(ANDROID_PLATFORM STREQUAL android-20)
    set(ANDROID_PLATFORM android-19)
elseif(NOT ANDROID_PLATFORM)
    set(ANDROID_PLATFORM android-9)
endif()
set(ANDROID_PLATFORM android-24)
set(ANDROID_PLATFORM_LEVEL 24)
string(REPLACE "android-" "" ANDROID_PLATFORM_LEVEL ${ANDROID_PLATFORM})
if(ANDROID_ABI MATCHES "64(-v8a)?$" AND ANDROID_PLATFORM_LEVEL LESS 21)
    set(ANDROID_PLATFORM android-21)
    set(ANDROID_PLATFORM_LEVEL 21)
endif()
if(NOT ANDROID_STL)
    set(ANDROID_STL gnustl_static)
endif()

回到android studio 重新sync一次,万事大吉.

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读