It's no any response after starting debugging - c++

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
}
}
]
}

Related

VScode C/C++ extension setup and configuration (macOS)

I am new to C++ and am still trying to set up the C/C++ extension in vscode.
I followed this tutorial provided by vscode official:
https://code.visualstudio.com/docs/cpp/config-clang-mac#_cc-configuration.
With the "lauch.json", "tasks.json", and "c_cpp_properties.json" files completed, I clicked the "play" icon, but my debugger still always show error with ";" and would open the launch.json file.
I think I am encountering the exact same problem with this post but its solution fails to solve mine: Mac VSCode Debugger always show error about ';' and ':'.
This is my code:
#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 << " ";
}
vector<string> aaa {"H", "E", "L", "L", "O"};
for (const string& word : aaa)
{
cout << word << " ";
}
cout << endl;
}
This 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
This is my tasks.json file:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
This is my c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Thanks!
The problem is that the program field is not right in launch.json, if your CPP name is your-program.cpp, you could set the program field to your-program.
Because the compiling output file name is "${fileDirname}/${fileBasenameNoExtension}" as you set in tasks.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "your-program", // set it to right name.
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}

Debugging C/C++ in VSCode when using different directories

I want to be able to debug my program when using different directories .
Let's take this example below :
Where main.c contains :
#include <stdio.h>
#include "lib/A.h"
int main()
{
test();
return 0;
}
A.h :
#pragma once
#include <stdio.h>
void test();
A.c :
#include "A.h"
void test()
{
printf("A\n");
printf("B\n");
}
When I try to debug this program(Breakpoint in the main program), I get this error below :
The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -1
However when if I remove A.c and write the whole function in the header file , debugging will work correctly :
A.h :
#pragma once
#include <stdio.h>
void test()
{
printf("A\n");
printf("B\n");
}
I might have to change something in tasks.json or settings.json, but I don't know what should be modified so that I could debug a program with A.c - A.h - main.c .
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json :
{
"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:\\msys64\\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"
}
]
}
I tried some solutions that I found but none of them worked that's why I wanted to post this question

VS Code - Only one cpp file from a project compiles

Okay so i have an error when am trying to run/compile the code.
Am guessing the problem is that my folder is not recognised as a project folder (i do not know how to solve that) and it compiles only one cpp file when it is needed to compile two of them.
error:
undefined reference to `Test2::fun()'
collect2.exe: error: ld returned 1 exit status
class.h
#include <iostream>
class Test2
{
public:
int a = 25;
void fun();
};
class.cpp
#include <iostream>
#include "class.h"
void Test2::fun()
{
std::cout << a;
}
main.cpp
#include <iostream>
#include "class.h"
int main()
{
Test2 e;
e.fun();
}
am not sure but that could be helpful to include my json files so... will include them
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": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\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
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I am new into vs code, sorry if that is a pretty easy to solve problem but am trying to figure it out and i can not.
As i said am thinking that vs code does not recognise it as a project and runs only main.cpp cause the error looks like that when am trying to run the code.
I was trying to run same thing on codeblocks and visualstudio2019 and it was working corretly so there is a problem with my vs code set up i guess.
Thanks for help and have a goood day <3

Errors while building and running C++ code in vscode using gcc. Tasks.json file

This is the code im trying to build and execute.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
}
i am able to compile and run it using the commands
directory path:// g++ -g hello.cpp
./a.exe
I now made a tasks.json file so that i don't have to run the above two commands everytime, and the .exe file is also named same as the .cpp file.
This is the error i'm facing:
image for build error in VSCode terminal
If anyone knows a fix for this, please help me out.
Let me know if you need the tasks.json file contents. I'll put them up here.
Tasks.json
{
"version": "2.0.0",
"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": "build",
"isDefault": true
}
}
]
}
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ (gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Try these Its working fine on my system
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile and run",
"type": "shell",
"command": "cls",
"args": [
";",
"g++",
"-g",
"${file}",
"-o",
"ans.exe",
";",
// "cls",
// ";",
"./ans.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.):(\\d+):(\\d+):\\s+(warning|error):\\s+(.)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
and 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}/ans.exe",
"args": [],
"stopAtEntry": false,
"externalConsole":true,
"cwd": "${workspaceFolder}",
"environment": [],
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

I'm Unable to debug my C++ code in VSCode

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