CMake 无法在 MinGW 上使用 GCC 进行编译

CMake unable to compile with GCC on MinGW

提问人:Pixelated Lagg 提问时间:11/10/2023 更新时间:11/10/2023 访问量:58

问:

每当我尝试运行 CMake 时,我都会收到此错误:

PS C:\Users\pixil\Desktop\P3D> cmake . -G "MinGW Makefiles"
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
CMake Error: Generator: execution of make failed. Make command was: C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_df89b/fast && 
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/msys64/ucrt64/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_ee3f9/fast && 
-- Check for working C compiler: C:/msys64/ucrt64/bin/gcc.exe - broken
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message):
  The C compiler

    "C:/msys64/ucrt64/bin/gcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeTmp
    
    Run Build Command(s):C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_ee3f9/fast && The system cannot find the file specified
    Generator: execution of make failed. Make command was: C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_ee3f9/fast && 




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:6 (project)


-- Configuring incomplete, errors occurred!
See also "C:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeOutput.log".
See also "C:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeError.log".
PS C:\Users\pixil\Desktop\P3D> 

CMakeError.log 只是重复以下内容:

Detecting C compiler ABI info failed to compile with the following output:
Change Dir: C:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeTmp

Run Build Command(s):C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_74e00/fast && The system cannot find the file specified
Generator: execution of make failed. Make command was: C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_74e00/fast && 



Determining if the C compiler works failed with the following output:
Change Dir: C:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeTmp

Run Build Command(s):C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_62e4a/fast && The system cannot find the file specified
Generator: execution of make failed. Make command was: C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_62e4a/fast && 

CMakeLists.txt:

set(CMAKE_GENERATOR, "mingw32-make")
cmake_minimum_required(VERSION 3.22.5)
set(CMAKE_C_COMPILER "C:/msys64/ucrt64/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/msys64/ucrt64/bin/g++.exe")

project(p3d)

file(GLOB SOURCES src/*.cpp)

add_executable(p3d ${SOURCES})


target_include_directories(p3d
    PRIVATE 
        ${PROJECT_SOURCE_DIR}/src
)

target_link_libraries(p3d
    PRIVATE 
        mingw32
        libfreeglut.a
        glut32.lib
)

这整个问题只是在我再次干净安装MinGW后才出现的。我已经看到几个解决这个一般问题的 StackOverflow 问题,但没有一个有任何适合我的解决方案。

我验证了我的 PATH 环境变量包含并且 GCC 确实有效。C:/msys64/ucrt64/bin

我都试过了:

set(CMAKE_C_COMPILER "C:/msys64/ucrt64/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/msys64/ucrt64/bin/g++.exe")

set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")

在 CMakeLists.txt 中

我所做的一切都没有解决错误。

GCC CMake mingw

评论

0赞 Tsyvarev 11/10/2023
闻起来像是不能自己起作用的。如果目录中有 Makefile,则可以尝试运行错误日志中记录的命令,并检查其输出和退出代码。C:/MinGW/bin/mingw32-make.exeC:/Users/pixil/Desktop/P3D/CMakeFiles/CMakeTmp
1赞 Tsyvarev 11/10/2023
顺便说一句,设置没用。首先,它修改的不是一个变量,而是一个变量(带有尾随逗号!其次,CMake 生成器不能在 : stackoverflow.com/questions/11269833/....set(CMAKE_GENERATOR, "mingw32-make")CMAKE_GENERATORCMAKE_GENERATOR,CMakeLists.txt
0赞 Pixelated Lagg 11/10/2023
等等,我刚刚意识到我没有目录!当我重新安装 MinGW 时,它没有创建该目录,现在位于 . 在我的 PATH 变量中,但 CMake 仍然找不到它。如何将路径传递给 CMake?C:/MinGWmingw32-make.exeC:\msys64\ucrt64\binC:\msys64\ucrt64\bin
0赞 Tsyvarev 11/10/2023
当您已构建项目,但环境已更改后,建议删除构建目录并重新运行。在您的情况下,您正在执行源代码内构建(当构建目录与源目录相同时,如暗示的那样)。CMake 强烈建议使用源代码外构建。例如,由 .在从源内版本切换到源外版本之前,请确保从源目录中删除文件。cmakecmake .mkdir build; cd build; cmake ..CMakeCache.txt
0赞 Pixelated Lagg 11/10/2023
删除缓存可以解决问题,谢谢:)

答: 暂无答案