CMake配置Android交叉编译遇错,请求技术协助
问题:CMake交叉编译Android C++库失败(Windows+VS2019+CMake GUI)
在Windows机器上使用CMake GUI v3.25.0,搭配Visual Studio 2019生成器交叉编译Android C++测试库,无论怎么调整工具链配置都报错,求助解决。
当前工具链文件内容
set(CMAKE_SYSTEM_NAME Android) set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a) set(CMAKE_ANDROID_NDK C:/Microsoft/AndroidNDK64/android-ndk-r16b/)
配置时的错误日志
Android: Targeting API '27' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a' Android: Selected Clang toolchain 'arm-linux-androideabi-clang' with GCC toolchain 'arm-linux-androideabi-4.9' The C compiler identification is Clang 5.0.300080 The CXX compiler identification is Clang 5.0.300080 Detecting C compiler ABI info Detecting C compiler ABI info - failed Check for working C compiler: C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe Check for working C compiler: C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message): The C compiler "C:/Microsoft/AndroidNDK64/android-ndk-r16b//toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: C:/Users/username/Documents/Code Projects/Android Test/build/CMakeFiles/CMakeScratch/TryCompile-zqgcqm Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe cmTC_b0f1d.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:m && Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. ANDROID_HOME=C:\Microsoft\AndroidSDK\25 ANT_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Apps\apache-ant-1.9.3 JAVA_HOME=C:\Program Files\Eclipse Foundation\jdk-8.0.302.8-hotspot NDK_ROOT=C:\Microsoft\AndroidNDK64\android-ndk-r16b testCCompiler.c clang.exe : warning : argument unused during compilation: '-mthumb' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj] clang.exe : warning : argument unused during compilation: '-mfpu=vfpv3-d16' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj] clang.exe : warning : argument unused during compilation: '-mfloat-abi=softfp' [-Wunused-command-line-argument] [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj] CLANGCOMPILE : error : unknown target CPU 'armv7-a' [C:\Users\username\Documents\Code Projects\Android Test\build\CMakeFiles\CMakeScratch\TryCompile-zqgcqm\cmTC_b0f1d.vcxproj] CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:4 (project) Configuring incomplete, errors occurred!
已尝试的操作
- 初始使用Visual Studio Android工具自带的NDK(r16b)
- 尝试过下载最新NDK并修改
CMAKE_ANDROID_NDK路径 - 试过使用NDK自带的
android.toolchain.cmake文件
问题原因
- 版本兼容性问题:CMake 3.25.0是较新版本,而NDK r16b过于老旧(对应Clang 5.x),两者的参数传递逻辑不匹配,导致Clang无法识别
armv7-a这个目标CPU参数。 - 生成器与工具链冲突:使用Visual Studio 2019生成器时,会自动注入Windows平台相关编译参数,和Android交叉编译的参数冲突,出现
-mthumb等参数未被使用的警告。
解决步骤
方案一:匹配兼容的NDK版本
CMake 3.25.x建议搭配NDK r21e及以上版本(NDK r21是最后一个支持armeabi-v7a的长期支持版本,后续版本已移除该ABI)。下载对应版本NDK后,更新工具链文件中的CMAKE_ANDROID_NDK路径。
方案二:正确使用NDK自带工具链文件
不要自行编写极简工具链文件,直接用NDK提供的配置:
- 在CMake GUI中点击
Add Entry,添加CMAKE_TOOLCHAIN_FILE,值为<你的NDK路径>/build/cmake/android.toolchain.cmake。 - 添加
ANDROID_ABI,值设为armeabi-v7a。 - 添加
ANDROID_PLATFORM,值设为android-27(或你需要的API等级)。 - 移除自行编写的工具链配置,重新点击
Configure。
方案三:改用Ninja生成器
- 在CMake GUI的生成器选择界面,选择
Ninja(需提前安装Ninja工具)。 - 配合NDK自带的工具链文件配置参数,重新执行
Configure和Generate。
额外注意事项
- 若必须使用armeabi-v7a,请勿使用NDK r23及以上版本(该ABI已被移除)。
- 检查环境变量中是否存在冲突的Android相关变量(如
NDK_ROOT),必要时临时清理或覆盖。
内容的提问来源于stack exchange,提问作者hjq
相关产品推荐
相关产品推荐

