How to setup VSCode debugger to work with cmake? - c++

I found a question here that is pretty close, but the C++ extension has been updated since then and the launch.json file that is created for you is very different and I'm not sure how to set it up. There also isn't a lot of info online to do this, so I'm hoping someone has done this before. My current project structure is like this:
MainProj
./build
CMakeLists.txt
main.cpp
otherClasses.hpp
And the cmake target that shows up in build after running ..cmake and make is called Main.
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
This is the tasks.json file from the other question
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cd build && cmake .. && make"
}
]
}

Related

How can I set up debugging C++ programs with command-line arguments in VS Code?

I am having a problem setting up my debugger in VSCode for C++. I wrote some code, then ran into some errors while running it so I decided to debug. I think thats when VSCode created the task.json file. Then I realized I wanted to use command line arguments and google/stackoverflow said to create a launch.json file which I did. It was empty so I pressed the add configuration button and got this.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
Now I tried to fill the placeholders but I can't do them without getting tons of errors after errors. Could someone please post an example of a working launch.json and how the file paths are supposed to work?
My main goal is to debug my code while using command line arguments.
Here is a sample from a project I currently use:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build_amd64/bin/tester_config",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}/build_amd64",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": []
}
]
}
If you post your launch.json with the filled in values then I might be able to tell you exactly what's wrong, because assuming you filled out everything correctly, you should be working.
You can find tutorials how to set up Visual Studio Code for C++ here:
https://code.visualstudio.com/docs/cpp/config-msvc
https://code.visualstudio.com/docs/cpp/config-mingw
A valid config should look something like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
As you can see, it's best to let the available extensions handle the setup, as getting all the compiler switches right can be tricky.
The full reference for tasks.json is found here:
https://code.visualstudio.com/docs/editor/variables-reference

VS Code on Windows debugger does not work with freopen

I found this issue, but it was closed https://github.com/microsoft/vscode/issues/112165
The problem is that VSCode can not read data from file when debugging c++ code.
Steps to Reproduce:
create a .cpp file
create a .txt with some data
use freopen or fopen, read data from the .txt file
debugging
the variables never change its value
Screenshots:
The value of T was supposed to be 3 (which it is under regular execution) but under debugging it is a random value 16.
Everything is default
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
}
]
}
I use
#ifdef SHERAJ
freopen("input.txt" , "r", stdin);
#endif
for competitive programming and VS Code on Mac debugger runs this perfectly fine. Please let me know a fix for this because I recently switched from Mac to Windows.
Okay, I get what is happening here. In the launch.json file, the "cwd" value is the path of the current working directory of the process being launched or debugged, this is where the input files are read from and output files and written to by default.
You should set it to the directory where the files you are working with are located.
Answer from here. Similar question: freopen() does not read / write to existing file, niether creates new file in vscode

VsCode debugging with c++17 fails?

I'm getting an issue where I can build and run a project with c++17 flags enabled, but I can't actually debug it. The debugger launches in vsCode, but all the c++ 17 features are red squiggled, and those lines never execute properly. I'm guessing it's a problem with my launch.json, but I can't figure out what it is that I'm missing...
I'm running windows 10, and here's the relevant configuration in vsCode:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-std=c++17",
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": ["-std=c++17"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
Welp - not much of an answer, but the issue isn't happening when I just use cl.exe as the compiler instead of gcc. I've since just stopped using gcc for this project after trying to get it to work for about an hour.
https://code.visualstudio.com/docs/cpp/config-msvc

The preLaunch 'C/C++:gcc.exe build active file' terminated with exit code -1

When ever I debug my code in Visual Studio Code I get
The preLaunch 'C/C++:gcc.exe build active file' terminated with exit code -1
I built my file using cpp.exe,
here is the launch.json file :
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "C://code//program.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}

VS code debugger doesn't open terminal

I'm trying to create a cpp environment on vs code. The build works, .exe gets generated, but the debugger doesn't seem to work. When I click on the run debug option, another terminal gets open with a message (attached in the screenshot). The code which I'm running is a simple hello world example. No terminal with Hello World is opened by the debugger. Hello world is supposed to be displayed in the screenshot of terminal 2 but another message gets printed, which I got no clue about.
Terminal 1
Terminal 2
This is the code for the config files:
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"sample.cpp"
],
"problemMatcher": []
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "C:\\Users\\Amey\\Desktop\\work\\Programming\\C++\\a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}