MinGW download and install

God horse situation? ? The screenshots have disappeared, forget it, I'm too lazy to make up.


I won't introduce what MinGW is here, there are many on Baidu.

Regarding the installation method of MinGW, you can also find a lot of them on Baidu, but they are all older versions. It is difficult for friends who are not good in English to look at it. Writing down your own installation process can help friends who are not good at English. , and make it easier for you to find in the future.

Well, without further ado, let's go to MinGW's official website: http://www.mingw.org

If English is better, you can read the introduction and installation method on the official website, which is very detailed.

There are two ways to install MinGW. One is to download the automatic installation tool. The advantage is that the installation is simple. High degree, you can choose according to your needs, the disadvantage is that manual download is troublesome, suitable for veterans.

Well, we came to the home page of the official website. For automatic installation, you can click Download Installer below to download the automatic installation tool. For manual installation, click Download below to enter the file download page.

The file download page is as shown below, and you can also click the link in the red box below to download the automatic installation tool.


Whether it is manual installation or automatic installation, it is necessary to enter this page. Due to the local area network of China, file downloads often fail during automatic installation, so you need to come here to download manually.

I will not talk about manual installation, just click in to download the files and libraries you want. For the tar.lzma file, you can use 7z to decompress it.

As for which files need to be downloaded, sorry, I don't know either, I downloaded them by automatic download.

Now let's talk about the use of the automatic installation tool.

This is the installation tool, double-click to run, click Install, select the installation path, other configurations are default, click Continue, and wait for the installation manager to be downloaded.


After the download is complete, click Continue to automatically run the download manager


Select Basic Setup, select the tool you use on the right, and click the mouse to see the introduction below.

mingw32-base is a basic C language compilation tool, including compilers, linkers, debuggers, runtime libraries, etc. It is required, and others can be used as needed.

msys-base is a tool that simulates a Linux terminal. If your cmd command fails, you can use this tool.

I don't know what mingw-developer-toolkit is, and Baidu didn't find any results. If you have any trouble, please let me know, thank you. Since I don't know what it's for, I usually choose it.

The other items are compilation tools for other languages, you can choose according to your own needs, my choice is as shown below.


After selecting, click Installation, select Apply Changes, click Apply to start the download


All that's left is to wait.

If there is an error in the download in the middle, first turn off the wrong one and download it again several times. If the re-download fails, go to the file download page to download manually, and then put the downloaded file in the \var\cache\mingw-get\packages folder in the MinGW installation directory , mine is D:\MinGW \var\cache\mingw-get\packages, and then download it again. At this time, the program verifies that all files have been downloaded and automatically decompresses.

Next, I will add environment variables. I won't say how to add them here. I will only say what values ​​need to be added here.

1. Add the bin folder path to the PATH variable, pay attention to the semicolon, my path is D:\MinGW\bin

2. Create a new environment variable LIBRARY_PATH, the value is the path of the lib folder, mine is D:\MinGW\lib

3. Create a new environment variable C_INCLUDE_PATH, the value of the include folder path, mine is D:\MinGW\include

At this point, the environment is installed, and then we will test it

Open cmd, or msys (MinGW\msys\1.0\msys.bat), and enter gcc -v on the command line. If the installation is successful, the version information of gcc will be returned.


Find a C program and compile it, find a piece of C code, as follows

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIntance,
	PSTR szCmdLine, int iCmdShow)
{
	static   TCHAR szAppName[] = TEXT("HelloWin");
	HWND     hwnd;
	MSG      msg;
	WNDCLASS wndclass;

	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszClassName = szAppName;
	wndclass.lpszMenuName  = NULL;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName,
			    TEXT("The Hello Program"),
			    WS_OVERLAPPEDWINDOW,
			    CW_USEDEFAULT,
			    CW_USEDEFAULT,
			    CW_USEDEFAULT,
			    CW_USEDEFAULT,
			    NULL,
			    NULL,
			    hInstance,
			    NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc;
	PAINTSTRUCT ps;
	RECT	    rect;

	switch(message)
	{
	case WM_CREATE:
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd,&rect);
		DrawText(hdc, TEXT("Hello, Windows!"), -1, &rect,
				DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hwnd,&ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,message,wParam,lParam);
}

Save the above code in "HelloWin.C" (note that the suffix C is an uppercase letter), then execute the command gcc HelloWin.C, and the returned result is as shown below


Compilation passes, but there are two errors when linking.

The second error "(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'" is really painful, just change the uppercase C of the suffix to a lowercase C.

For the first error "(.text+0x67): undefined reference to `GetStockObject@4'", just add the parameter "-mwindows".

Ok, let's try again, first rename "HelloWin.C" to " HelloWin.c " , and then execute the command "gcc HelloWin.c -mwindows ", the returned result is as shown below


OK, the compilation is passed, a.exe is generated in the same directory, let's try it out


The resulting a.exe runs fine.

In addition, it should be noted that the Makefile can also be used, and the writing method is exactly the same as that of Linux. The difference is that the executed command is not make, but mingw32-make.

Of course, you can also rename the mingw32-make.exe file in the MinGW\bin directory to make.exe, and you can use the make command to execute the Makefile



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473337&siteId=291194637