1. 问题描述

在使用cmake 3.22构建项目,执行cmake命令出现如下错误
CMake Error: CMake was unable to find a build program corresponding to “Ninja”. CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
– Configuring incomplete, errors occurred!

2. 问题分析

通过 cmake --help 发现 ninja 带了*号,表示为默认的构建工具
Generators
The following generators are available on this platform ( marks default):*
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be “Win64” or “ARM”.

MinGW Makefiles = Generates a make file for use with
mingw32-make.
*Ninja = Generates build.ninja files.

cmake --help 也提示了 参数-G 可以指定构建器名称,
-S = Explicitly specify a source directory.
-B = Explicitly specify a build directory.
-G = Specify a build system generator.
–build   = Build a CMake-generated project binary tree.
–install  = Install a CMake-generated project binary
tree.

3. 解决方法

因为机器上没有装ninja,所以导建构建失败,要么给机器装上ninja要么指定其他的构建器,因为没有使用Ninja的需求,所以使用指定其他构建器的方式

这里选择 MinGW或VS2017 进行构建,因此命令参数改为

#生成构建文件
cmake -G “MinGW Makefiles” -B ./MinGW_build -S ./
cmake -G “Visual Studio 15 2017” -B ./MSVC_build -S ./
#根据已构建的文件进行编译, 这里也可以调用对应编译器进行编译 如make或cl
cmake --build ./MinGW_build
cmake --build ./MSVC_build

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐