MinGW and MSYS2 Installation
1. What are MinGW and MSYS2
MinGW is the abbreviation of “Minimalist GNU for Windows”, which contains a lot of develop tools. Now we often use the MinGW-64 version. If we need to compile some C/C++ files, the MinGW has what we need.
However, install MinGW is a little cumbersome, the more common way is install MSYS2.
The MSYS2 is the abbreviation of “Minimal SYStem 2”. It is a “micro Linux-like develop environment”, which contains many Linux tools includes bash, ls, grep, sed, awk, find, make, git…
1.1 Why do we install MSYS2 instead of MinGW directly?
After installing the MSYS2, we get a magnificent advantage: the
pacman(Package Manager).
e.g. The traditional way to install the GCC:
- Step1. download the compressed file(zip or tar file…)
- Step2. decompress the file.
- Step3. configure the PATH
If you need to update your GCC, you need to redownload the file and cover it .
But if you use the pacman, you just need one command to finish the installation:
pacman -S mingw-w64-ucrt-x86_64-gcc
1.2 Summary
So let’s conclude the COT:
1 | We need to compile C/C++ files |
2. Install MSYS2
Download MSYS2.
After installing this, run this command to install gcc and gdb in the UCRT64 window:
1 | pacman -S mingw-w64-ucrt-x86_64-gcc |
1 | pacman -S mingw-w64-ucrt-x86_64-gdb |
Check:
1 | test@ѩ▒ UCRT64 ~ |
1 | test@ѩ▒ UCRT64 ~ |
check in the git bash:
1 | test@▒ѩ▒ MINGW64 ~ |
the gcc command is not found, so we need to add the PATH.
find the MSYS2’s urt64’s bin folder path:
D:\softwares\MSYS2\ucrt64\bin
add it to the Windows’ Environment Variable PATH.
Then restart the git bash window, check:
1 | test@ѩ▒ MINGW64 ~ |
3. Configure VS Code
3.1 Choose the default compiler for c/cpp
Open the VS Code, find the gear at the left-bottom. Click and choose
the settings, then search C_Cpp.default.compilerPath. Open
the setting.json, and fulfill the value:
1 | "C_Cpp.default.compilerPath": "" |
1 | "C_Cpp.default.compilerPath": "D:/softwares/MSYS2/ucrt64/bin/gcc.exe" |
3.2 Create task.json and launch.json file
Create a new folder and open it in vscode:
E:\test_msys2_c
then press Ctrl+Shift+P search for
:Tasks: Configure Default Build Task, select:
Create tasks.json file from template, select:
Others, create tasks.json file.
Amend the json file:
1 | { |
Note: I set the git bash as the default terminal for vscode, so I change this part:
1 | "args": [ |
into:
1 | "args": [ |
Press Ctrl+Shift+D search for
:create a launch.json file, select:
C++ (GDB/LLDB), create launch.json file.
1 | { |
3.3 Test
create a hello.c file in the last folder:
1 |
|
4. Unexpected Python Confliction
Because GitBash cannot find gcc, so I added
D:\softwares\MSYS2\ucrt64\bin into the global variable
PATH. Now gcc is available globally.
Unexpected side-effect:
D:\softwares\MSYS2\ucrt64\bin not only has
gcc.exe, gdb.exe and make.exe,
but also has python.exe. But I have installed a python
already.
Confliction:
Priority:
1. System PATH: `D:\softwares\MSYS2\ucrt64\bin` (complimentary python by MSYS2)
1. User PATH: `%USERPROFILE%\...\WindowsApps` (installed by win store)
Solution:
Rename the complimentary python of MSYS2:
1
2
3
4
5
6
7
8
9
10cd /d/softwares/MSYS2/ucrt64/bin
mv python.exe python-msys2.exe
mv python3.exe python3-msys2.exe
mv python3.14.exe python3.14-msys2.exe
mv pythonw.exe pythonw-msys2.exe
mv python3w.exe python3w-msys2.exe
mv python3.14w.exe python3.14w-msys2.exe
mv python-config python-msys2-config
mv python3-config python3-msys2-config
mv python3.14-config python3.14-msys2-configVerification:
1
2
3test@ѩ▒ MINGW64 /d/softwares/MSYS2/ucrt64/bin
$ which python
/c/Users/test/AppData/Local/Microsoft/WindowsApps/pythonVS Code Setting Repair:
CTRL + SHIFT + P->Preferece -> OPEN USER SETTING1
"python.defaultInterpreterPath": "C:\\Users\\test\\AppData\\Local\\Microsoft\\WindowsApps\\python3.12.exe"