Skip to content

Instantly share code, notes, and snippets.

@urbans0ft
Last active September 12, 2022 11:58
Show Gist options
  • Select an option

  • Save urbans0ft/e633e0878daa98eaf72ff3c3d562f09e to your computer and use it in GitHub Desktop.

Select an option

Save urbans0ft/e633e0878daa98eaf72ff3c3d562f09e to your computer and use it in GitHub Desktop.
Setup Win32 API Development Environment with MinGW, Visual Studio Code and CMake

You may skip the tl;dr section an start with the Setup instantly.

tl;dr

This tutorial describes how to setup a Win32 API1 development environment on Windows (where else 😉). The goal is to create a setup with publicly available (open-source) tools:

  • CMake2
  • Visual Studio Code3
  • MinGW-64 Builds4

Setup

  1. Download the latest Mingw-w64 release4 (x86_64-XX.x.x-release-win32-seh-rt_v10-rev0.7z).
    1. Extract the content to C:\Dev
    2. Add C:\Dev\mingw64\bin to your %PATH% environment variable.
  2. Download the latest CMake Windows x64 ZIP2 package.
    1. Extract the content to C:\Dev
    2. Add C:\Dev\cmake\bin to your %PATH% environment variable.
  3. Install Visual Studio Code3.
    1. C/C++ Extension Pack

Proof of Concept

>cmake --version
cmake version 3.23.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
>g++ --version
g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Minimal Win32 Program

To start from scratch and test our setup we need two files to start with:

  • main.cpp
    The sample source code to test compilation.

    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
    {
        return 0;
    }
    
  • CMakeLists.txt The CMake configuration file.

    cmake_minimum_required(VERSION 3.20)
    
    # set the project name
    project(Win32)
    
    # add the executable
    add_executable(Win32 main.cpp)

Let assume you set up the following structure:

>tree /f
Auflistung der Ordnerpfade
Volumeseriennummer : 000001F7 D052:013A
C:\Dev\Win32\Minimal.
    CMakeLists.txt
    main.cpp

Open vscode with (C:\Dev\Win32\Minimal) and press Ctrl+Shift+D to open the Run perspective. Press Run and Debug. You're asked to select an environment.

grafik

Choose C++ (GDB/LLDB) and select the Default configuration.

grafik

This'll create a launch.json file. Replace the content by the following code5:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // Resolved by CMake Tools:
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    // add the directory where our target was built to the PATHs
                    // it gets resolved by CMake Tools:
                    "name": "PATH",
                    "value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
                }
            ],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

We're almost there. Open the command palette (Windows: Ctrl+Shift+P) and enter CMake: Configure. You should be able to select the Compiler we just installed at C:\Dev.

grafik.

You should see a lot of output in the correspondant window.

This is it: Open main.cpp and press F5 to Start Debugging.

The setup of the development environment has been successful.

FAQ

  • Which Mingw-w64 build to I have to use?
  • How do I add directories to the %PATH% variable?
  • Why do I have to use WinMain instead of wWinMain

References

Footnotes

  1. https://docs.microsoft.com/en-us/windows/win32/api/

  2. https://cmake.org/download/ 2

  3. https://code.visualstudio.com/ 2

  4. https://github.com/niXman/mingw-builds-binaries/releases 2

  5. https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/debug-launch.md#debug-using-a-launchjson-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment