Setting up Win32 for Mingw

I'm running into a lot of errors when I'm trying to run Win32 program examples. I want to make sure I have everything setup correctly.

Most examples online are using Visual Studio but I'm using VS Code with MinGW. I have the Windows SDK installed but I don't know if I have to do something to link it with MinGW or if I have to link a library file during compiling. I noticed that MinGW has a built in Windows.h, that doesn't need to be disabled somehow does it?

I'm just shooting in the dark.
Maybe, tell us what the errors are? I use MinGW regularly and might know.

And print out what version of mingw you are using.
It's like g++ --version or g++ -v or something like that.
The original MinGW project is dead (no updates in years, website is down). Mingw-w64 has taken over.

MinGW-w64 does not need or use separate Windows SDK.

It already comes with all the header files (e.g. Windows.h) and associated import libraries that you'll need to use the Win32 API.

I strongly recommend to install MinGW-w64+MSYS via the MSYS2 project:
https://www.msys2.org/
Last edited on
I'm running into a lot of errors

Copying'n'pasting even some of the errors would be helpful. Just saying "I get lots of errors" isn't very useful to diagnose what the problems are.

I do not use VS Code, but I do use Code::Blocks with MinGW. Actually MinGW-64 and other variants since as others have said the original MinGW is dead, gone and buried.

Win32 API has been changed to Windows API since support for 64-bit Windows apps was added to the API/SDK.

Modern MinGW (and the variants) has the Windows API headers and start-up code needed to create Windows API GUI apps.

There is a huge difference between the start-up code added to executables for console mode apps and Windows API apps. VS Code may be defaulting to using the Console Mode start-up code. There should be some way for changing that.

Don't ask me how, since I don't use VS Code.
Ok. I sorry I didn't post the errors, it seemed like I was getting different errors in every example I downloaded. I thought it might be easier to review that I had setup everything correctly.

I have installed MinGW with MSYS2 and I did notice there was already a windows.h in there. The error I'm getting running some example code from MSLearn is

1
2
3
4
5
Starting build...
C:\msys64\mingw64\bin\gcc.exe -fdiagnostics-color=always -g C:\Users\kaega\OneDrive\Programming\Learning\win32\first_window.cpp -o C:\Users\kaega\OneDrive\Programming\Learning\win32\first_window.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status


I'm guessing I need to link a library.
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'

This error means that the linker is missing the "main" function of your application 😏

Be aware that WinMain() (or wWinMain()) is the entry-point used for GUI (graphical Windows-based) applications:
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain

Console applications use main() (or wmain()) as their entry-point instead:
https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-170

So, are you building a GUI or a Console application? And does your code contain the appropriate entry-point function?

Note: With GCC/Mingw-w64, the option -mconsole or -mwindows can be used to select the type (Console or GUI) of the application!

(I honestly don't know how you set up VS Code to pass the required option to GCC, but you will probably figure it out)

________

You can try to manually run the following command from the MSYS2 terminal (mingw32.exe in MSYS2 directory):
gcc -mconsole -o program.exe main.c

...or, if you use C++:
g++ -mconsole -o program.exe main.cpp

(replace "main.c" with whatever your actual source code file is named; you can append more source code files, if required)
Last edited on
I'm literally just trying to get this example code from this site to work

https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program

I've literally just opened VS Code, created a new cpp file, pasted the code in, clicked run, selected G++ compiler. It created what I assume is a default task.json.

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
39
40
41
42
43
44
45
46
47
48
49
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-mwindows",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
        }
    ],
    "version": "2.0.0"
}


I didn't have the -mwindows in there before but I've added it since and am still having the same issue.

I'm really starting bare bones here. I'm likely just missing a super basic step and can't find it on the tutorials I'm looking at. What do I need to make a basic GUI Win32 program work?
1
2
3
I'm literally just trying to get this example code from this site to work

https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program 

This obviosuly is a GUI application.

Also, it uses the wWinMain() function as its entry point (note the leading "w"):
1
2
3
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    [...]

So, you need to build this code as a Windows and Unicode application, i.e. the following options should be used with Mingw-64/GCC:
-mwindows -municode

You can probably add those to the "args" array in your task.json file 😊
Last edited on
Thank you.

It works :-)
Topic archived. No new replies allowed.