Qt Creator 使用带有 CMake 的 MinGW 编译器

Qt Creator using MinGW compiler with CMake

我正在尝试将 Qt Creator 用于不使用 Qt 库的 C++ CMake 项目。

我正在关注 official guide,但它根本不起作用。这是我的步骤:

  1. 将我的 MinGW bin 目录添加到 PATH
  2. 运行 QtCreator 和设置 CMake。
  3. 打开一个非常基本的 CMakeLists.txt 文件。
  4. Select "Ninja (Desktop)" 作为 CMake 生成器。
  5. 从 CMake 获取错误。

您可能已经注意到,官方指南中实际上根本没有提到第4步。我已经习惯了 CMake,因此我问自己:

为什么 Qt Creator 不提供普通的 "MinGW Makefiles" 生成器?

最后是终极问题:

如何让 Qt Creator 通过 CMake 使用 MinGW 编译器?


已回答问题:

根据官方指南和往常一样,当您正确设置 PATH 时,应该不会发生第 5 步。

为什么CMake找不到PATH中设置的编译器?

是 Ninja Generator 造成了这些错误,如果您使用 CMake 手动执行也会发生同样的事情。

CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file: D:/Programming/C++/Test/SupportQt/build/CMakeFiles/3.0.2/CMakeCXXCompiler.cmake
CMake Error at CMakeLists.txt:1 (project):
  No CMAKE_C_COMPILER could be found.

  Tell CMake where to find the compiler by setting 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):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting 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.


CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file: D:/Programming/C++/Test/SupportQt/build/CMakeFiles/3.0.2/CMakeCCompiler.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.-- Configuring incomplete, errors occurred!

查看配置菜单,看看我是否忘记设置某些东西,我偶然发现了编译器设置页面。然后我当然也在那里添加了我的编译器,但它什么也没做。

为什么CMake在使用CMake时不使用编译器列表?

它确实使用编译器列表,如果您在工具包选择中选择了正确的编译器。

像往常一样,如果你在弄清楚后不久才寻求帮助,我现在觉得很愚蠢...

为什么Qt Creator在使用CMake时不使用编译器列表?

确实如此,但前提是您将 Kit 设置为使用正确的编译器!

为什么CMake找不到PATH中设置的编译器?

这其实是Ninja Generator with CMake的问题。如果直接使用 CMake,也会发生同样的事情。

为什么 Qt Creator 不提供普通的 "MinGW Makefiles" 生成器?

确实如此,但前提是您在套件中选择了 MinGW 编译器!

如何让 Qt Creator 通过 CMake 使用 MinGW 编译器?

通过正确设置所有内容而不过度依赖官方指南。