Windows installation of MinGW and simple usage tutorial
What is MinGW?
MinGW is the abbreviation of Minimalist GNU for Windows. It is a collection of freely available and freely redistributable Windows-specific header files and import libraries using the GNU toolset, allowing you to generate native Windows programs on GNU/Linux and Windows platforms without the need for a third-party C runtime (C Runtime) Library. MinGW is a set of files and port libraries that allow console-mode programs to use Microsoft's standard C Runtime library (MSVCRT.DLL). This library is available on all NT OS and on all Valid for Windows OS above the Windows 95 release. Using the basic runtime, you can use GCC to write console mode compliant American Standards Organization (ANSI) programs. You can use the C Runtime (C Runtime) extension provided by Microsoft, with the basic runtime. When combined with time, you can have full rights to use both CRT (C Runtime) and Windows API functions (this content comes from Baidu Encyclopedia).
MinGW-w64 is the 64-bit version of MinGW. MinGW has transferred its development efforts to 64-bit systems many years ago. The 32-bit MinGW has not been updated for many years. The following installation processes are all based on 64-bit systems.
how to download
You can go to MinGW's official website to download: Downloads - MinGW-w64
MinGW hosts it on sourceforge, you can also go to sourceforge to download: MinGW-w64 - for 32 and 64 bit Windows - Browse /Toolchains targeting Win64/Personal Builds/mingw-builds/8.1.0/threads-posix/seh at SourceForge .net
If you download from sourceforge, just select the install.exe file to download. This is the method I will use next. Installation using executable installation files is more friendly to novices. You can also download and install MinGW without installation. Finally, if you use codeblock with MinGW, you can jump directly to the environment variable configuration process.
how to install
Installer installation
After downloading the install.exe file, run the software and enter the following page:
Click next to enter the installation configuration;
Select the MinGW version number to install. If there are no special requirements (if it is just for learning), just install the latest version:
Select the system architecture, X86_64 is a 64-bit system, and i686 is a 32-bit system.
Select the operating system interface protocol: select posix for developing Unix-like (linux, macOS, Unix, etc.) system programs, and win32 for developing Windows programs.
Select exception handling type: Exception handling is very important in development. During the development process, most of the time will be spent on handling various exception situations. seh is a new invention, while sjlj is ancient. seh has better performance, but does not support 32-bit. sjlj has good stability and supports 32-bit. It is recommended to choose seh for 64-bit operating systems.
Select the installation path and click next to enter the installation process (you need to be online to download the corresponding data of MinGW).
After the installation is complete, you can go to the corresponding installation path to see a folder like this:
Installation-free version
We can directly download the corresponding MinGW compressed package, and decompress it to get the folder after installation above. Taking the above installation version as an example, the installation version we want to choose is as shown below:
Environment variable configuration
After installing MinGW, you need to configure the environment variables. Just add the bin folder in the above folder to the path variable in the environment variables:
check
Enter gcc-v or g++ -v in the command prompt. If there is output, the configuration is successful.
Easy to use
edit code
Use Notepad or a code editor to write a program (Notepad is not recommended here, I use vscode).
First, use the command line to enter the directory where you want to create the file for subsequent operations.
Compilation process
The compilation process is divided into four steps: preprocessing, compilation, assembly, and linking.
1. Preprocessing
Preprocessing mainly processes preprocessing commands such as "#include" and "#define" in the source file.
The main tasks completed by preprocessing are:
(1) Delete #define and expand the macro;
(2) To process conditional compilation instructions, the preprocessor first determines the conditions and then modifies the source code according to the conditions;
(3) Delete comments;
(4) Add line number and file name identification to facilitate debugging
(5) Delete "#include" and insert the corresponding header file;
Use the g++ -E test.cpp -o test.icommand to get the test.i file after preprocessing
2.Compile
In the process of generating assembly code, use the command g++ -S test.i -o test.s. to generate the assembly file test.s file. Of course, you can also get the assembly file directly from the test.cpp file.
3.Compilation
Convert assembly code into machine instructions and generate target binary code.
Use the command g++ -c test.s -o test.o to generate the test.o file
4.Links
Convert target files into executable files by linking library files
Use the command g++ test.o -o test.exe
Of course, under normal circumstances, you can directly use g++ test.cpp -o test to generate an executable program.
Run program
Finally, use the command test.exe to run the program.
You can also directly click on the exe file to run it.