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

直接下载

安装

  1. 安装时选择好安装位置(不能有中文或空格),其他默认即可。
    mingw-install-1
  2. 安装完成后打开MinGW,勾选mingw32-basemingw32-gcc-g++,分别是C/C++编译器。
    mingw-install-2
  3. 点击Installation -> Apply Changes,在弹出窗口中点击Apply
    mingw-install-3
    mingw-install-4
  4. MinGW添加到系统环境变量
    edit-environment-path-1
    edit-environment-path-2
    edit-environment-path-mingw
  5. 在终端中输入gcc -v查看是否安装成功。
  6. 进入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

cmake-download

安装

  1. 双击运行下载的.msi文件。
    cmake-install-1
  2. 选择第二个将CMake添加到系统环境变量,桌面快捷方式根据个人习惯选择。
    cmake-install-2
  3. 选择安装位置。
    cmake-install-3
  4. 在终端中输入cmake --version查看是否安装成功。

系统环境变量修改后要重启终端才能生效


使用CMake编译

  1. 创建一个test文件夹。
1
mkdir test&&cd test
  1. 在文件夹中新建main.cCMakeLists.txt文件,并在其中分别写入以下内容。
1
2
3
4
5
6
7
// main.c
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
1
2
3
4
5
6
7
8
9
10
# CMakeLists.txt
cmake_minimum_required(VERSION 3.27)
project(test)

# 这里的"C:/MinGW/bin"路径是你MinGW的路径
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")

set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")

add_executable(test main.c)
  1. test文件夹下创建build文件夹,并用CMake编译。
1
2
3
mkdir build&&cd build
cmake .. -G "MinGW Makefiles"
make

错误

  1. 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,使用安装包安装,不要下载压缩包手动添加环境变量

  1. CMake Error: Target DependInfo.cmake file not found
    运行:
1
make

输出:

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下载和安装教程