Build a C++ development environment based on VSCode under Windows (including the detailed process of integrating MinGW64 and CMake)

Recently I want to write C ++ , installed VisualStudio 2022, and tossed for a long time. For a person who is used to VSCode, the IDE always feels too cumbersome. So I searched various information on the Internet, pondered by myself, and built a C++ lightweight development environment based on VSCode and CMake under Windows.

Concrete construction steps

1. Download and install VSCode

VSCode download address (AzureCDN acceleration address)
First agree to the installation agreement:

In the next step, tick all the boxes for convenience:

Then click install to:

2. Install the necessary VSCode plugin

It is recommended to install the Chinese Simplified Chinese Sinicization Package, C/C++, and C/C++ Extension Pack officially provided by Microsoft. The C/C++ Extension Pack extension will automatically install plugins such as CMake and CMake Tools for us.

3. Download and decompress MinGW64, CMake

The full name of MinGW is Minimalist GNU on Windows, that is, the well-known GCC compiler and its related compilation and debugging components in the Linux/Unix environment are ported to Windows. With this toolset we can compile, debug, run and package C/C++ projects.
MinGW64 download address (the latest version of gcc 13.1.0 on github)
If the github download is slow, you can consider GitHub file acceleration: https://ghproxy.com

After the download is complete, unzip it to a specified directory (remember this directory, it will be used later)

Then download CMake, CMake is a modern C ++ compilation and packaging tool, and many open source C/C ++ are used. Personally, I feel a bit like Java’s Maven build tool, but the functions are not as complete as Maven. CMake download address (the latest version 3.27.0 of CMake official website)

After the download is complete, just unzip it to a specified directory (also remember this directory)

The final highlight is to use the above two decompression directories to configure environment variables, open Settings -> System -> System Information:

Click System Information -> Advanced System Settings:

Click on Environment Variables:

You can choose both the upper and lower paths

Configure the installation directory\bin in the Path variable, and then click OK all the way and you’re done!

4. Add the configuration in the .vscode directory

Use VSCode to open an empty folder, create a folder called .vscode (note that there is an English period in front. Don’t miss it), and create c_cpp_properties.json, There are four files, settings.json, launch.json and tasks.json, the contents of which are as follows:

  • c_cpp_properties.json

    {<!-- -->
        "configurations": [
            {<!-- -->
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "windowsSdkVersion": "10.0.17763.0",
                "compilerPath": "C:\Users\22820\apps\mingw64\bin\g++ .exe", // this Specify the address of g++.exe installed by yourself
                "cStandard": "c11",
                "cppStandard": "c++11",
                "intelliSenseMode": "${default}",
                "configurationProvider": "ms-vscode.cmake-tools"
            }
        ],
        "version": 4
    }
    
  • settings.json

    {<!-- -->
      "cmake.cmakePath": "C:\Users\22820\apps\cmake-3.27.0\bin\cmake.exe" // Here specify the cmake.exe address you installed
    }
    
  • launch.json

    {<!-- -->
        // Use IntelliSense for relevant properties.
        // Hover to see descriptions of existing properties.
        // For more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {<!-- -->
                "name": "(gdb) start",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/cpp_learn", // add the project name configured in project() in CMakeLists.txt after build (CMakeLists.txt will be introduced later)
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {<!-- -->
                        "description": "Enable tidy printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    },
                    {<!-- -->
                        "description": "Set the disassembly style to Intel",
                        "text": "-gdb-set disassembly-flavor intel",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask":"Build",
                "miDebuggerPath": "C:\Users\22820\apps\mingw64\bin\gdb.exe" // Specify yourself here Installed gdb.exe address
            }
        ]
    }
    
  • tasks.json

    {<!-- -->
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "options": {<!-- -->
            "cwd":"${workspaceFolder}/build"
        },
        "tasks": [
            {<!-- -->
                "label": "cmake",
                "type": "shell",
                "command": "cmake",
                "args":[
                    ".."
                ]
            },
            {<!-- -->
                "label":"mingw32-make", // use mingw32-make under windows, directly use make under Linux/Unix
                "group":{<!-- -->
                    "kind": "build",
                    "isDefault": true
                },
                "command":"mingw32-make",
                "args": [
     
                ]
            },
            {<!-- -->
                "label":"Build",
                "dependsOrder": "sequence",
                "dependsOn":[
                    "cmake",
                    "mingw32-make"
                ]
            }
        ]
    }
    

5. Create a CMakeLists.txt file and write a main.cpp test

Create CMakeLists.txt (equivalent to Maven’s pom.xml) in the root directory of the project. Don’t study the specific content carefully. Copy mine first:

cmake_minimum_required(VERSION 3.15) # Specify the minimum version of cmake
project(cpp_learn) # specify the project name
include_directories(cpp_learn include) # specify the header file directory
aux_source_directory(cpp_learn src) # specify the source file directory
add_executable(cpp_learn src/main.cpp) # Specify the program entry

Then create main.h and main.cpp to the include folder and src folder in the root directory respectively:

After exiting VSCode and re-entering it again, you will find that CMake has started to work, and a build folder is generated under the project root directory, which contains the content to be compiled. Among them, cpp_learn.exe is the final compiled product.

It also supports gdb breakpoint debugging, you can start debugging after hitting the bug below the breakpoint (or press F5)!

Summary

It is not easy to build a C ++ environment by using VSCode! But for developers, the most important thing is to have the courage to try and toss around. You must have the spirit of pony crossing the river. But having said that, I can finally get rid of the bulky guy of Visual Studio 2022 hhh, and VSCode can also customize the color theme, which is not too comfortable for my face control~~

Easter eggs

VSCode good-looking theme recommendation:

  • Darcula Theme: Inspired by the color scheme of JetBrains family barrels (Intellij IDEA, Web Storm, PyCharm, etc.), it is a re-enactment of the classic
  • One Dark Pro: One of the most popular themes of VSCode, refreshing and anti-fatigue, favored by the majority of programmers
  • Atom One: Choose from light and dark themes! And they’re all healthy