MinGW
MinGW: A native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All of MinGW’s software will execute on the 64bit Windows platforms.
下载 MinGW - Minimalist GNU for Windows
直接下载
安装
安装时选择好安装位置(不能有中文或空格),其他默认即可。
安装完成后打开MinGW,勾选mingw32-base
,mingw32-gcc-g++
,分别是C/C++编译器。
点击Installation
-> Apply Changes
,在弹出窗口中点击Apply
。
将MinGW
添加到系统环境变量
在终端中输入gcc -v
查看是否安装成功。
进入MinGW安装位置C:\MinGW\bin
,将mingw32-make.exe
复制一个出来改成make.exe
方便使用。
到这里你可以使用gcc -o main main.c
来编译C程序。
CMake 下载 CMake下载地址 ,选择正确的平台,一般是x64。
直接下载cmake-3.27.4-windows-x86_64.msi
安装
双击运行下载的.msi
文件。
选择第二个将CMake
添加到系统环境变量,桌面快捷方式根据个人习惯选择。
选择安装位置。
在终端中输入cmake --version
查看是否安装成功。
使用CMake编译
创建一个test
文件夹。
在文件夹中新建main.c
和CMakeLists.txt
文件,并在其中分别写入以下内容。
1 2 3 4 5 6 7 #include <stdio.h> int main () { printf ("Hello World!\n" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 cmake_minimum_required (VERSION 3.27 )project (test )set (CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe" )set (CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe" )add_executable (test main.c)
在test
文件夹下创建build
文件夹,并用CMake编译。
1 2 3 mkdir build&&cd build cmake .. -G "MinGW Makefiles" make
错误
The C compiler is not able to compile a simple test program.
运行:
1 cmake .. -G "MinGW Makefiles"
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 PS D:\Trrrrw\文档\C\test\build> cmake .. -G "MinGW Makefiles" -- The C compiler identification is GNU 6.3.0 -- The CXX compiler identification is GNU 6.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: D:/MinGW/bin/gcc.exe -- Check for working C compiler: D:/MinGW/bin/gcc.exe - broken CMake Error at D:/Trrrrw/应用/命令行工具/cmake/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "D:/MinGW/bin/gcc.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: 'D:/Trrrrw/文档/C/test/build/CMakeFiles/CMakeScratch/TryCompile-qihtzh' Run Build Command(s): D:/Trrrrw/应用/命令行工具/cmake/bin/cmake.exe -E env VERBOSE=1 D:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_22fc3/fast D:/MinGW/bin/mingw32-make.exe -f CMakeFiles\cmTC_22fc3.dir\build.make CMakeFiles/cmTC_22fc3.dir/build mingw32-make.exe[1]: Entering directory 'D:/Trrrrw/文档/C/test/build/CMakeFiles/CMakeScratch/TryCompile-qihtzh' 系统找不到指定的路径。 CMakeFiles\cmTC_22fc3.dir\build.make:76: recipe for target 'CMakeFiles/cmTC_22fc3.dir/testCCompiler.c.obj' failed mingw32-make.exe[1]: *** [CMakeFiles/cmTC_22fc3.dir/testCCompiler.c.obj] Error 1 mingw32-make.exe[1]: Leaving directory 'D:/Trrrrw/文档/C/test/build/CMakeFiles/CMakeScratch/TryCompile-qihtzh' Makefile:126: recipe for target 'cmTC_22fc3/fast' failed mingw32-make.exe: *** [cmTC_22fc3/fast] Error 2 CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:2 (project) -- Configuring incomplete, errors occurred!
解决: 重新安装cmake,使用安装包安装,不要下载压缩包手动添加环境变量
CMake Error: Target DependInfo.cmake file not found
运行:
输出:
1 2 3 4 5 6 CMake Error: Target DependInfo.cmake file not found make[2]: *** No rule to make target 'D:/Trrrrw//C/test/main.c', needed by 'CMakeFiles/test.dir/main.c.obj'. Stop. CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/test.dir/all' failed make[1]: *** [CMakeFiles/test.dir/all] Error 2 Makefile:90: recipe for target 'all' failed make: *** [all] Error 2
解决: 文件夹路径不能有中文
MinGW下载和安装教程