如何阻止 cmake 用 cl 编译?

Posted

技术标签:

【中文标题】如何阻止 cmake 用 cl 编译?【英文标题】:How do I stop cmake from compiling with cl? 【发布时间】:2019-08-28 19:17:28 【问题描述】:

我正在尝试使用 cmake 构建一个 c++ 项目。我在 Windows 上使用崇高的文本。这是我的 CMakeLists.txt 配置:

project(Arena)

cmake_minimum_required(VERSION 3.0)

set(CMAKE_C_COMPILER C:\MinGW\bin)
set(CMAKE_CXX_COMPILER C:\MinGW\bin)

当我运行cmake . 命令时,我收到以下错误消息:

-- Building for: NMake Makefiles
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:1 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:1 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

错误信息说没有找到cl编译器,这是因为我没有安装它。但我已经安装了 MINGW GNU 编译器集合 (GCC)。这些在我的PATHC:\MinGW\bin。如何配置 cmake 以使用我机器上已有的编译器进行编译?

愚蠢的问题警报:我也很困惑为什么错误消息是在谈论 NMake generator with Visual C++,因为我没有使用 Visual Studio?

尝试 1:

cmake . -G "MinGW Makefiles"这样运行cmake命令,得到以下错误:

CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  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

【问题讨论】:

你也应该发布你的 CMakeLists.txt 配置。 检查我更新的问题@Romen。 我认为您的系统 PATH 环境变量中没有 mingw 二进制文件。 当我运行命令 echo %PATH% 时,我 C:\MinGW\bin; 存储二进制文件的元素之一,对 @drescherjm? 应该没错,那个文件夹里有make程序吗? 【参考方案1】:

我也很困惑为什么错误消息是在谈论带有 Visual C++ 的 NMake 生成器,因为我没有使用 Visual Studio?

您的 CMake 似乎认为“NMake Makefiles”是默认的 generator type,这意味着 CMake 配置步骤正在为 NMake 输出项目/makefile。

如何配置 cmake 以使用我机器上已有的编译器进行编译?

运行 CMake 时,指定生成器:

cmake . -G "MinGW Makefiles"

您还应该做一些事情来改进/修复您的 CMakeLists.txt:

    在项目行之前指定最低版本

    添加以下行以告诉 CMake 您的 MinGW 二进制文件在哪里:

set(CMAKE_MAKE_PROGRAM <your make program path>)
set(CMAKE_C_COMPILER <your c compiler path>)
set(CMAKE_CXX_COMPILER <your c++ compiler path>)

如果 MinGW 已正确添加到您的 PATH,则此步骤应该是不必要的。

    指定项目使用的语言。
project(Arena LANGUAGES CXX)

【讨论】:

检查我的问题以了解尝试此操作时发生的错误。 当我将路径添加到编译器和-G "MinGW Makefiles"时出现同样的错误。 你所说的make程序是指C:\Program Files\CMake\bin? @TomFinet,make是MinGW提供的程序,这和cmake不是同一个程序。 set(CMAKE_**** &lt;path&gt;) 中的路径应该指向 .exe,而不是 exe 所在的文件夹。 @TomFinet,您的C:\MinGW\bin 文件夹中有哪些程序?只是mingw-get.exe 吗?你看到mingw32-make.exe了吗?【参考方案2】:

您需要在 CMakeList.txt 文件中指定您的编译器:

set(CMAKE_C_COMPILER <your c path >)
set(CMAKE_CXX_COMPILER <your c++ path here>)

【讨论】:

我尝试添加 set(CMAKE_C_COMPILER \MinGW\bin) set(CMAKE_CXX_COMPILER \MinGW\bin) 但它给了我完全相同的错误。

以上是关于如何阻止 cmake 用 cl 编译?的主要内容,如果未能解决你的问题,请参考以下文章

CMake - 如何在打包期间阻止执行安装脚本?

我可以阻止 GoogleTest-build 修改全局 CMAKE_CXX_FLAGS 变量吗?

为啥 cl.exe(Visual Studio 编译器)无法使用 CMake(错误报告)编译项目?

如何强制 cmake 在没有完整路径的情况下使用 cl.exe?

Intellij 不断编译 TypeScript(非常慢) - 如何阻止这种行为?

如何阻止java反编译