— Programming, C++, editor, vscode, mingw, gcc, gdb — 2 min read
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.
Below is the list of things to be installed before we start setting up vscode.
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.
1base-devel2msys2-devel3mingw-w64-i686-toolchain4mingw-w64-x86_64-toolchain
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.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.
<MSYS2_DIR>/mingw64/include/c++/
.c_cpp_properties.json
.CTRL + Shift + P
and selecting C/Cpp: Select a configuration
by typing in the selection box..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": 431}
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=7335583 // for the documentation about the tasks.json format4 "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 infos27 "problemMatcher": {28 "base": "$gcc",29 "fileLocation": "absolute"30 }31 }32 ]33}
To setup debugger the program
and the miDebuggerPath
is the important values to configured properly.
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=8303875 "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": true24 }25 ],26 "sourceFileMap": {27 "/g": "G:/"28 }29 }30 ]31}
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
.
Tasks: Run Task
and select the Build tasks or name given in the taks.json say "label": "Build Current File"
in current setup.