Skip to content
Nishanth-blag

Setup C++ in VSCode Windows- GCC + GDB

Programming, C++, editor, vscode, mingw, gcc, gdb2 min read

Why I am writings this

I think a simple google search will show articles related to this but those were not helpful to me when i setup on my windows environment. Especially setting up the gdb debugger. Which throws the error file not found as here: https://github.com/Microsoft/vscode-cpptools/issues/1546#issuecomment-365368570 and fixed it with sourceFileMap in launch.json.

And also this post can be a good way to archive my vscode setup for windows.

Prepare for setup

Below is the list of things to be installed before we start setting up vscode.

  1. MSYS2
  2. GCC

So why are these two packages used? Firstly MSYS2 is required to install GCC. Also GCC can be installed in many other ways using cygwin or mingw. Usually other tutorials suggest using cygwin or mingw but MSYS2 provides the minimal linux subsystem and also comes with pacman to install packages. Since i use pacman on my linux distro i was quite comfortable using this and think that others will feel the same.

  • Get MSYS2 from here https://www.msys2.org/
  • Setup MSYS2, install the packages as show in the instruction on the home page and also set any environment variables if mentioned.
  • Make sure the packages below are installed using pacman.
1base-devel
2msys2-devel
3mingw-w64-i686-toolchain
4mingw-w64-x86_64-toolchain
  • If you have installed mingw-w64-i686-toolchain or mingw-w64-x86_64-toolchain then gcc will be included in that package. Now that the requird packages are installed lets set the various config files required to make use of g++ and gdb in vscode.

Setup VSCode

To setup vscode to work with C++ we need to make sure the standard include paths are set for intellisense. Build tasks is setup which uses g++ from the installed GCC package. Debugger has to be setup in the launch.json file.

Setup include paths for intellisense

  • On installing GCC the standard header files are available in the directory <MSYS2_DIR>/mingw64/include/c++/.
  • These are to be setup in the c_cpp_properties.json.
  • A default config can be generated using the command palette CTRL + Shift + P and selecting C/Cpp: Select a configuration by typing in the selection box.
  • The generated config is placed in .vscode folder in the roor directory.
1{
2 "configurations": [
3 {
4 "name": "Win32",
5 "includePath": [
6 "${workspaceFolder}/**",
7 "C:/msys64/mingw64/include/c++/8.2.0",
8 "C:/msys64/mingw64/include/c++/8.2.0/x86_64-w64-mingw32",
9 "C:/msys64/mingw64/include/c++/8.2.0/backward",
10 "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.2.0/include",
11 "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.2.0/include-fixed"
12 ],
13 "browse": {
14 "path": [
15 "${workspaceFolder}/**",
16 "C:/msys64/mingw64/include/c++/8.2.0",
17 "C:/msys64/mingw64/include/c++/8.2.0/x86_64-w64-mingw32",
18 "C:/msys64/mingw64/include/c++/8.2.0/backward",
19 "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.2.0/include",
20 "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.2.0/include-fixed"
21 ]
22 },
23 "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
24 "compilerPath": "C:/msys64/mingw64/bin/g++",
25 "cStandard": "c11",
26 "cppStandard": "c++17",
27 "intelliSenseMode": "clang-x64"
28 }
29 ],
30 "version": 4
31}

Setup Build tasks

Similar to the above scenario if there is no tasks.json generate one from the command palette by selecting Tasks: Configure Task. As per my use case i want to compile the current file active in the editor, to achieve this iam using ${file} variable provided by vscode config variables. Also here ${filebasenameNoExtension} is used to set the output for the compile file without any extension.

1{
2 // See https://go.microsoft.com/fwlink/?LinkId=733558
3 // for the documentation about the tasks.json format
4 "version": "2.0.0",
5 "tasks": [
6 {
7 "label": "Build Current File",
8 "type": "shell",
9 "command": "C:/msys64/mingw64/bin/g++",
10 "args": [
11 // Ask msbuild to generate full paths for file names.
12 "-g",
13 "-Wshadow",
14 "-Wall",
15 "-Wextra",
16 "-std=c++17",
17 "-o",
18 "${fileBasenameNoExtension}",
19 "'${file}'"
20 ],
21 "group": "build",
22 "presentation": {
23 // Reveal the output only if unrecognized errors occur.
24 "reveal": "silent"
25 },
26 // Use the standard MS compiler pattern to detect errors, warnings and infos
27 "problemMatcher": {
28 "base": "$gcc",
29 "fileLocation": "absolute"
30 }
31 }
32 ]
33}

Setup Debugger using gdb

To setup debugger the program and the miDebuggerPath is the important values to configured properly.

  • It is set in such a way that the debugger gets the name of the compiled file of the current active tab in the editor.
  • Also here for some reason the path was set incorrectly so i had to use sourceFileMap.
1{
2 // Use IntelliSense to learn about possible attributes.
3 // Hover to view descriptions of existing attributes.
4 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 "version": "0.2.0",
6 "configurations": [
7 {
8 "name": "(gdb) Launch",
9 "type": "cppdbg",
10 "request": "launch",
11 "program": "${workspaceFolder}/${fileBasenameNoExtension}",
12 "args": [],
13 "stopAtEntry": false,
14 "cwd": "${workspaceFolder}",
15 "environment": [],
16 "externalConsole": true,
17 "MIMode": "gdb",
18 "miDebuggerPath": "C:/msys64/usr/bin/gdb.exe",
19 "setupCommands": [
20 {
21 "description": "Enable pretty-printing for gdb",
22 "text": "-enable-pretty-printing",
23 "ignoreFailures": true
24 }
25 ],
26 "sourceFileMap": {
27 "/g": "G:/"
28 }
29 }
30 ]
31}

How to use this setup

The config for build and debug is configured in such a way that it uses the filename of the current active file in the editor. So when a build task is run the file to be compiled will be fetched from ${file} variable provided by vscode. Similarly for gdb the compiled files for current open file is fetched from ${fileBasenameNoExtension} since the -o flag was set with that in the tasks.json.

  • To run the compile tasks open command palette and select Tasks: Run Task and select the Build tasks or name given in the taks.json say "label": "Build Current File" in current setup.
  • After build is finished successfully goto the debug section and run debug, but make sure the current active file is the one you want to debug.