How to set up code path when debug C++ in Visual Studio - c++

I am following this official Visual Studio site to start developing C++ in Visual Studio: https://code.visualstudio.com/docs/languages/cpp#:~:text=C%2FC%2B%2B%20support%20for%20Visual%20Studio%20Code%20is%20provided,use%20the%20keyboard%20shortcut%20%28Ctrl%2BShift%2BX%29.%20Search%20for%20%27C%2B%2B%27. I have MinGW installed as suggested.
I can build the code and it runs correctly.
When I set a break point and run debug, the break point is ignored. The following messages appears several times in the Debug Console:
PS C:\Users\Cheng\Documents\Temp\HelloWorld> & 'c:\Users\Cheng\.vscode\extensions\ms-vscode.cpptools-1.13.9-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ccgsybc5.exz' '--stdout=Microsoft-MIEngine-Out-bwaebfti.urx' '--stderr=Microsoft-MIEngine-Error-rwnjcwal.lr2' '--pid=Microsoft-MIEngine-Pid-mj2o2p2s.eil' '--dbgExe=/msys64/usr/bin/gdb.exe' '--interpreter=mi'
When I set "stopAtEntry" to true in lauch.json, I see the following log in the Debug Console indicating the correct code path:
Thread 1 "HelloWorld" hit Breakpoint 1, main () at C:\Users\Cheng\Documents\Temp\HelloWorld\HelloWorld.cpp:5
But a new file pop up, with empty content and the following path:
C:\Users\Cheng\Documents\Temp\HelloWorld\C\Users\Cheng\Documents\Temp\HelloWorld\HelloWorld.cpp
The content of the launch.json is the following:
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/msys64/usr/bin/gdb.exe",
"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
}
]
},
]
}
I think that it is just an issue of specifying the correct debug path.
Can anyone help me on this configuration issue? Very much appreciated your help.
CP

Related

Visual studio code with cmake on Windows, how to set compiler and debug configuration

I have Windows 7 with few different versions of Visual Studio and MinGW. When I create a cmake project and open it in Visual Studio Code it get configured automatically to use Visual Studio 14 compiler, I also have Visual Studio 17 and 19 installed, version 17 have "Desktop development with C++" workload. installed.
I would like to know how to set which compiler to use when running initial configuration (when build folder is empty) and how to create a debug configuration. When I run plain debug button that do not require the configuration the break point do not trigger.
I have tried with these config files in launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"env": {
"Path": "${env:Path};D:\\Installation\\oracClient 19\\instantclient_19_14\\"
},
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "c:\\Users\\marko.grujic.CHIP\\.vscode\\extensions\\ms-vscode.cpptools-1.8.4\\debugAdapters\\bin\\WindowsDebugLauncher.exe",
"logging": {
"engineLogging": true
},
"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
}
]
},
{
"name": "(Windows) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"env": {
"Path": "${env:Path};D:\\Installation\\oracClient 19\\instantclient_19_14\\"
},
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"logging": {
"engineLogging": true
},
"console": "externalTerminal"
}
]
Non of these work, (gdb) Launch config do not work because the code was compiled with Visual Studio compiler and I am trying to use MinGW debugger (This is assumption).
(Windows) Launch also do not work but I have no idea why, it does run indefinitely but none of breakpoints work.

VS code: Unable to start debugging - Program path is missing or invalid

I'm trying to use the debugger in vs code. I have problems to activate and run it.
This is my code:
#include <gtest/gtest.h>
// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
VSCode reports that the program path is missing or invalid, even though I copied the address of the .exe file to the program path of launch.json, then GDB reports that the .exe file not in file format recognized. Any idea would be appreciate much. TY
This is the code in my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [
""
],
"stopAtEntry": false,
"cwd": "c:/Users/ABC/Desktop/Test",
"environment": [],
"program": "c:/Users/ABC/Desktop/Test/hello_test.exe",
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
"externalConsole": false,
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

What to in VScode do after launch JSON file C++

I'm learning a basic to c++ in Vscode but I'm having issues with debugging, I followed everything downloaded MinGW, edit the environment path variable value to C:\MingGW\Bin. I tried but when I try to run it opens a launch.JSON what do I do after that? When I run > start debugging I get two options, I click on C++(GDB/LLDB). It opens a JSON file it reads :
{
// 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
}
]
}
]
}
I don't know what do to next please help...

Debugger not working for C++ Visual Studio Code

I am trying to debug my C++ code with gdb, but every time I set a breakpoint and press F5, it just doesn't do anything. I hover over the breakpoint and it says "Module containing this breakpoint has not yet loaded or the breakpoint address could not be obtained." even if I put a breakpoint right on main! Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"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
}
]
}
]
}
in addition, the little menu that pops up, the step over, step into, step out buttons are all greyed out. I can only pause, restart, or stop the debugging (that isn't happening).
I compile and run my program with this
g++ inputbuf.cc lexer.cc parser.cc -o a.out && a.out < tests/no_errors/test_01.txt > output.txt
Also, the double backslash for the gdb path "\\" vs "/" in launch.json doesn't do anything.

Unable to start debugging with GDB from VSCode because of error: Unexpected GDB output from command "-exec-run"

I keep getting Unexpected GDB output from command "-exec-run" error in the debug console while I try to run my c++ code which includes a custom class I made and takes a .txt file as an input in the same path.
I'm using the command g++ -g class.h class.cpp mycode.cpp -o mycode.exe in the terminal and just try to debug the code with F5.
I don't get this error when I'm executing a cpp file with no libraries included
Here is my launch.json file
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
"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
}
]
}
]
}
I've heard downgrading gdb or g++ could help but I could not understand how can I downgrade them. If you think that's the solution a simple explanation for how to downgrade would be much appreciated.