I am learning debugging C++ file from Udemy .I want to debug a code but I was getting output in external console , so I set external terminal to false . Now I am not getting any output while debugging neither in terminal nor in output option . Please help me I am new to debugging
C++ Code
#include<iostream>
using namespace std;
int main(){
int num{100};
cout<<"Hello from Project 2 "<<endl;
cout<<"Enter a Number : ";
cin>>num;
cout<<"The number you entered : "<<num;
return 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": "C:\\Users\\mahir\\OneDrive\\Documents\\C++ debugging Udemy\\project2\\main2.exe",
// There I have got error Property keys must be doublequotedjsonc
"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
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
Related
I wrote a c++ example program in VS code on ubuntu 16. The example si to read file and write file. However, the compiler reports that it can not find the file path. I don' know what happened. Here is my main.cpp file:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
using namespace std;
void test01(){
char* filename ="/home/mark2/Desktop\\source.txt";
ifstream ism(filename, ios::in); // only read function to open the file
//ifstream ism;
//ism.open(filename, ios::in);// only read function to open the file
if(!ism){
cout<<"file open failed"<<endl;
}
//read file
char ch;
while(ism.get(ch)){
cout<<ch;
}
//file has beem opened, now close the file
ism.close();
}
int main(){
test01();
return 0;
}
And here is my 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": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I don't what should I do to next. Could you please give me some advice?
Thanks!
I'm trying to debug a simple Cpp program that reads an input file and make a graph from it. However, when trying to debug on VSCode, this error pops up:
Unable to open 'strtol_l.c': Unable to read file '/build/glibc-ZN95T4/glibc-2.31/stdlib/strtol_l.c' (Error: Unable to resolve non-existing file '/build/glibc-ZN95T4/glibc-2.31/stdlib/strtol_l.c').
Here is my program:
int main(int argc, char *argv[])
{
unordered_map<int, vector<int>> graph;
ifstream input("input1.txt");
string row;
while (getline(input,row)) { >>> where the error is
istringstream iss(row);
int node1, node2;
if (iss >> node1 >> node2) {
graph[node1].push_back(node2);
graph[node2].push_back(node1);
}
}
input.close();
}
here 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": ["input1.txt"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
A picture of the error:
Any hint?
I'm trying to debug my C++ file but whenever I press the Run >> start debugging option, it just runs and quit without any errors or anything. My program is not running...
Here's my code:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int rows, question;
std::string length;
cin >> rows;
for (int i = 0; i < rows; i++)
{
cin >> length;
}
return 0;
}
Here's what it shows in the terminal after clicking on debug:
cmd /C "c:\Users\intel\.vscode\extensions\ms-vscode.cpptools-0.29.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-oxw2rz3u.v3w --stdout=Microsoft-MIEngine-Out-5me5n3jp.wq2 --stderr=Microsoft-MIEngine-Error-wg1xnokk.ddw --pid=Microsoft-MIEngine-Pid-kfv0gezm.4wp --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "
C:\Users\intel\Desktop\Python programs>
Here's what it shows in the output window:
Here's my tasks.json:
{
"version": "2.0.0",
"_runner": "terminal",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
Here's 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/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
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
]
}
I am using a windows machine and a MinGW for C++
I use VSCode to develop in C++ too, but not tryed to use with MinGW yet. I use it with MSVC and GCC (Linux).
My launch configuration is sligh different from yours, using GCC.
{
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing"
}
]
},
"name": "Run gcc (linux) x64 debug",
"request": "launch",
"program": "/projects/CPP/GHDWS/build/gcc-linux/x64/debug/GHDWS",
"cwd": "/projects/CPP/GHDWS/build/run",
"args": [],
"preLaunchTask": "Build gcc x64 debug",
"type": "cppdbg",
"externalConsole": false
},
I made my own system build, that automatically configure VSCode to your project. If you're willing, you can try use it.
CppMagic
According to the instruction: https://code.visualstudio.com/docs/cpp/config-mingw, .vscode files are set. Then, I add the MinGW64 path to the system path. But, when I start debugging, VS Code hangs and no finish, I also scan the Console by the steps: Help->Toggle Developer Tools, and the Console displays the following error, and I don't know how to solve.
enter image description here
The followings are my source code and both JSON files:
// source code file
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
// 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": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I try to setup helloworld project with vscode and gdb for cpp.
I uses msys2.
It all sets just fine, project compiles and runs, but i have problem with debugging it.
Here 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/usr/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
and my demo project
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
But when i run debug, instead source file it show just empty file, i figure out that new file created in
C:\c\Users\user\projects\helloworld\
so there a problem with path there.
How to fix it?
I use wrong gdb.
You need install other version of gdb:
pacman -S mingw-w64-x86_64-gdb
and use it
"miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",