CMake调用两个NDK编译Ceres库失败,求原因及解决办法
Android平台构建Ceres库编译错误排查与解决
我正尝试按照官方文档在Android平台构建Ceres库,但遇到编译错误无法推进。我并非构建工具专家,若问题较为基础还请见谅。目前核心问题是:CMake同时使用了我通过Android Studio安装的NDK,以及Visual Studio开启安卓开发功能后自带的NDK,导致编译测试程序失败。
执行的CMake命令
cmake -DCMAKE_TOOLCHAIN_FILE="C:\Users\l.gagliano\AppData\Local\Android\Sdk\ndk\16.1.4479499\build\cmake\android.toolchain.cmake" -DEigen3_DIR=D:\DEV\ceres\eigen\build -DANDROID_ABI=arm64-v8a -DANDROID_STL=c++_shared -DANDROID_NATIVE_API_LEVEL=android-21 -DBUILD_SHARED_LIBS=ON -DMINILOG=ON D:\DEV\ceres\ceres-solver-2.1.0
编译错误输出
-- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: C:/Users/l.gagliano/AppData/Local/Android/Sdk/ndk/16.1.4479499/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- Check for working C compiler: C:/Users/l.gagliano/AppData/Local/Android/Sdk/ndk/16.1.4479499/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:/Users/l.gagliano/AppData/Local/Android/Sdk/ndk/16.1.4479499/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: D:/DEV/ceres/ceres-android/CMakeFiles/CMakeScratch/TryCompile-lab0em Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe cmTC_366ff.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\Professional\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 In file included from <built-in>:327: <command line>(3,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [D:\DEV\ceres\ceres-android\CMakeFiles\CMakeScratch\TryCompile-lab0em\cmTC_366ff.vcxproj] #define __ANDROID_API__ 21 ^ <command line>(1,9): note: previous definition is here #define __ANDROID_API__ 19 ^ 1 warning generated. C:\\Microsoft\AndroidNDK64\android-ndk-r16b\toolchains\x86_64-4.9\prebuilt\windows-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin\ld: error: cmTC_366ff.dir\Debug\testCCompiler.o: incompatible target clang.exe: error: linker command failed with exit code 1 (use -v to see invocation) C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Application Type\Android\3.0\Android.Common.targets(119,5): error MSB6006: "clang.exe" exited with code 1. [D:\DEV\ceres\ceres-android\CMakeFiles\CMakeScratch\TryCompile-lab0em\cmTC_366ff.vcxproj] CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:60 (project) -- Configuring incomplete, errors occurred! See also "D:/DEV/ceres/ceres-android/CMakeFiles/CMakeOutput.log". See also "D:/DEV/ceres/ceres-android/CMakeFiles/CMakeError.log".
问题原因
- 环境变量冲突:系统环境变量中的
NDK_ROOT指向Visual Studio自带的NDK,但你指定的CMAKE_TOOLCHAIN_FILE是Android Studio安装的NDK路径,导致两个NDK混合调用。 - 架构不匹配:你指定了
ANDROID_ABI=arm64-v8a,但链接器使用的是Visual Studio NDK的x86_64工具链,生成的目标文件架构不兼容,引发链接错误。 - API级别重定义:Visual Studio的NDK默认设置
__ANDROID_API__=19,与你指定的android-21冲突,导致宏重定义警告,进而触发后续编译失败。
解决办法
- 清理冲突环境变量:
打开系统环境变量设置,暂时清空或删除NDK_ROOT、ANDROID_HOME变量,打开新的命令提示符(确保环境变量生效)后重新执行CMake命令。 - 明确指定NDK路径:
在CMake命令中添加-DANDROID_NDK参数,强制指定Android Studio的NDK路径,覆盖环境变量影响:cmake -DCMAKE_TOOLCHAIN_FILE="C:\Users\l.gagliano\AppData\Local\Android\Sdk\ndk\16.1.4479499\build\cmake\android.toolchain.cmake" -DANDROID_NDK="C:\Users\l.gagliano\AppData\Local\Android\Sdk\ndk\16.1.4479499" -DEigen3_DIR=D:\DEV\ceres\eigen\build -DANDROID_ABI=arm64-v8a -DANDROID_STL=c++_shared -DANDROID_NATIVE_API_LEVEL=android-21 -DBUILD_SHARED_LIBS=ON -DMINILOG=ON D:\DEV\ceres\ceres-solver-2.1.0 - 切换构建生成器:
若不需要Visual Studio构建安卓项目,可指定CMake使用Ninja或Makefiles生成器,避免调用MSBuild:
注意:使用Ninja需提前安装该构建工具。cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE="C:\Users\l.gagliano\AppData\Local\Android\Sdk\ndk\16.1.4479499\build\cmake\android.toolchain.cmake" -DANDROID_NDK="C:\Users\l.gagliano\AppData\Local\Android\Sdk\ndk\16.1.4479499" -DEigen3_DIR=D:\DEV\ceres\eigen\build -DANDROID_ABI=arm64-v8a -DANDROID_STL=c++_shared -DANDROID_NATIVE_API_LEVEL=android-21 -DBUILD_SHARED_LIBS=ON -DMINILOG=ON D:\DEV\ceres\ceres-solver-2.1.0
内容的提问来源于stack exchange,提问作者user8469759
相关产品推荐
相关产品推荐

