Debug GNU using VS Code without GDB - c++

Over the weekend I had installed a gcc through the MSYS2 bash. I set it up in VS code and have it working properly. I even had the GDB working (yes I know this is a debugger). But, my main question is, is it possible to use the debugging function in VS code to debug rather then GDB. Pressing F5 it pulls up the launch.json file and gives me launch: program 'enter program name, for example c:\School\a.exe' does not exist .After some research I see you give it a file to the args to allow it run in debugger. When I do this though I can't seem to either give it the right file or make it work overall. I am also using a.exe rather than a.out. I'm unsure if this has effect.
{
// 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": ["C:\\School\\CSE340\\project2\\main.cpp"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

VS Code does not have an internal debugger (see here). You need to use GDB or the visual studio debugger (if you have the latter).
In your launch.json you need to modify the entries:
"program": this is the path to the program you want to debug, i.e. your compiled program (can be a relative path to your project folder)
"miDebuggerPath": this is the path to GDB
"args": these are arguments, you want to pass to your programm for debugging purposes, i.e. you can leave this blank
So the launch.json file for you would look something like 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": "${workspaceFolder}\\CSE340\\project2\\main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // Path where your gdb.exe is located
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
${workspaceFolder} is a path variable to your workspace and seems to point to C:\\School\\, so maybe you'll need to modify the value of "program" to point to the application that you want to debug. You could also specify the absolute path to your program.
Also, do not forget to compile your code with debug-flags (-g), these are needed by GDB in order to step through the code. For example:
g++ -g main.cpp -o main.exe

Related

I can't debug C in VSCode/CodeBlocks or Any IDE

This is my 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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
It was GDB.exe here earlier, But it said it couldn't find it , Even I couldn't find it..
So I put gdb32.exe as i was able to find that out , But it still didn't debug anything
i am fed up of this problem now...
Code Blocks says Please specify executable path,It can't be found
Please help, Thanks
Code::Blocks can be installed without MinGW, which is okay as MinGW-w64 is much better maintained. But MinGW-w64 needs to be installed separately.
You can download a standalone MinGW-w64 from https://winlibs.com/ and the site also explains on how to add this compiler and debugger to the settings.

VSCode not compiling c++

Im running Ubuntu MATE and i program in C++. I wanted to transfer to vscode so i installed it. But overall i cant really compile or debug anything. This works in terminal to run a code.
g++ HelloWorld.cpp -o HelloWorld.o
./HelloWorld.o
But if i want to go through button to compile this thing happens. What should i do? Im adding launch.json down below.
{
// 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": "cpp - 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": "C/C++: cpp build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
If anybody in the future wonder what was the solution, compile c++ under linux with g++ and then press ctrl+f5. That one solved my problem, couldnt figure it out for 2 days.

Proble in debuging c++ code with visual studio code, with lunch.json file

I have Problem in debugging c++ programs in Visual studio code. Although, debugging was working before. I had not change any configuration or setting. When i opened visual studio code today then i saw that error.
Image
lunch.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": true,
"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": "g++.exe build active file"
}
]
}
I have not changed anything in system/vc code/ configuration etc.
Then, why debugging stops working.
The program setting specifies the program you want to debug. Here it is set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will be helloworld.exe.
The main problem is vccode is not able to find the current file for debugging.
These are some links that might work for you.
Docs
Configuration

How to solve : "launch : program "executable.out" does not exist" in VS Code when debugging with gdb

I am following that tutorial from the VScode team :
https://code.visualstudio.com/docs/cpp/config-wsl#_debug-helloworldcpp
I have run and built another c++ project.
It compiles but the project kraches at runtime, so I decide to debug it.
So i want to debug and I use gdb as proposed by this good tutorial.
When I execute the debugging with "F5", I have that 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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "executable.out",
"args": [],
"stopAtEntry": true,
"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"
}
]
}
I have modified the "program" section to put the name of my executable ( executable.out) , which is located in the "workspaceFolder"
So I don't understand the error message I get when I do "F5" :
launch : program "executable.out" does not exist
Could someone explain to me how to solve that ?
I thought a lot before posting this question.
Thanks a lot in advance.
Best regards
Use absolute path in program:
"program": "${workspaceFolder}/executable.out"
I assume you are using Linux, since you are using g++ compiler.
Did you compile your cpp file to "executable.out", e.g.
g++ main.cpp -o executable.out
Your launch.json configuration look fine to me. It should work.

VS Code does not show output from a program

I've created a c++ project on ubuntu in visual studio code.
It launches the program using gdb mode, but does not show anything in Output. When I launch program from console, the output is present. I tried inserting "console" property, but vscode tells me, that it is forbidden (from similar issue with node https://github.com/Microsoft/vscode/issues/30842).
How can I enable vscode to show the output from my program?
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
You config is correct. The program output should be in the DEBUG CONSOLE.
Menu | Debug Console or Ctrl+Shift+Y.
The program was launched in external terminal and due to the configuration of my screen I didn't see it. Everything works fine.
Add this line in setting.json in code-runner.executorMap
"cpp": "g++ -o $fileNameWithoutExt $fileNameWithoutExt.cpp && ./$fileNameWithoutExt"