I am debugging the following code in VSCode:
int main() {
char arr1[] = {'t', 'e', 's', 't', '\0'};
char arr2[] = "test";
int arr3[] = {1, 2, 3};
double arr4[] = {1.1, 2.2, 3.3};
}
The elements of the first two arrays show as:
116 '<error reading variable>
101 '<error reading variable>
115 '<error reading variable>
116 '<error reading variable>
0 '<error reading variable>
Whereas the elements of the last two arrays show the actual values of the numbers.
The contents of the launch.json file are:
{
"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:\\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"
}
]
}
What can I do so that the first two arrays show the characters in the array?
The following solution resolved the issue:
I found a workaround in this chinese blog
https://blog.csdn.net/m0_46304383/article/details/113487503
(translated with google transator). It seems a Windows 10 UTF-8
problem. I hope it can also help you #georgetian3.
The workaround is: "Windows Settings" -> "Time and Language" ->
"Region" -> "Other Date -> Time and Regional Settings" -> "Change Date
-> Time or Number Format" -> "Manage" -> "Change System Regional Settings" -> Uncheck "Beta version: Use Unicode UTF-8 to provide
global language support" -> restart the computer
Related
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"
}
]
}
I am getting error after running debugger like
c:\Users\USER.vscode\extensions\ms-vscode.cpptools-1.7.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-vomttuky.aw4' '--stdout=Microsoft-MIEngine-Out-5gcky3n5.jua' '--stderr=Microsoft-MIEngine-Error-1xr5gc00.po0' '--pid=Microsoft-MIEngine-Pid-w3xqmfyg.lmp' '--dbgExe=C:/MinGW/bin/gdb.exe' '--interpreter=mi'
whenever i try to debug simple cpp file
Hello.cpp -
#include <bits/stdc++.h>
using namespace std;
int sqr(int a){
return a*a;
}
int main (){
int a =10;
int b =2;
int c =a*b;
cout << sqr(c);
return 0;
}
I have configured my launch.json file 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}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"logging": { "engineLogging": true },
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
i am using
VSCode - 1.61.2
MiGW - 6.3.0
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 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",