Use integrated terminal for debugging C++ - c++

I would like to use the integrated terminal for debugging C++ files in VSCode on my Mac. I have the C/C++ extension added and enabled globally.
Here is the launch.json that I am using:
{
// 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": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
I have tried adding "console": "integratedTerminal" to this with no change. When debugging a program, the terminal output says:
> Executing task: /usr/bin/clang++ -fdiagnostics-color=always -std=c++17 -g /Users/kris/learncpp/chapter_03/debugTest4.cpp -o /Users/kris/learncpp/chapter_03/debugTest4 <
Terminal will be reused by tasks, press any key to close it.
Hitting any key here closes the terminal and drops me into a bash shell. Is there another setting I am missing somewhere?

Related

Unable to start debugging. Unexpected GDB output from the command -environment-cd "Path" . No such file or directory

I've just started to use VSCode, I meet this error and try some ways to fix but it doesn't work.
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": "C/C++: 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:\\TDM-GCC-64\\gdb64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
Problem fixed by doing these steps below.
Go to Windows Control Panel (You can do this by opening File Explorer and in the path type 'Control Panel' (without quotes) and hit Enter);
From the Control Panel, click on 'Clock and Region'.
Click Region. A new screen will open.
In the screen that opened, click on Administrative and search for the button "Change system location".
Check the option: "Beta: Use Unicode UTF-8 for world language support"
Restart the computer.
Posted on behalf of the question asker
the problem was with Launch.json
the path files should not have any spaces like in my case "prog files" was error and i replaced and rename folder with "progfiles"
"program": "D:\\Progfiles\\vscode\\hello.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:\\Progfiles\\vscode",
"environment": [],

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.

How to set gdb as debugger for the C/C++ extension pf VSCode on MacOS?

the C/C++ Extension of Microsoft for VSCode allows to set a launch.json file with which you can set how to debug and run your C++ code. By default it has lldb as debugger for MacOS.
I wonder how to set gdb as debugger instead of lldb.
I tried and it shows me:
Unable to start debugging. GDB exited unexpectedly with exit code 134 (0x86).
This is how my launch.json file looks like:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++-9 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"osx": {
"miDebuggerPath": "/usr/local/bin/gdb",
"MIMode": "gdb"
},
"preLaunchTask": "C/C++: g++-9 build active file"
}
]
}
Make sure you have gdb installed.
For that you can use:
brew install gdb
Then make the gdb binary is certified. Because:
... modern Darwin kernels restrict the capability to assume
control over another process (here, for the purpose of debugging it),
since that capability is a boon to malware. In order for said
taskgated to grant access to gdb, the latter must be fitted with
entitlements, which consist of digitally-signed metadata inside the
gdb binary.
From: https://sourceware.org/gdb/wiki/PermissionsDarwin
To create a certification follow these instructions:
https://sourceware.org/gdb/wiki/PermissionsDarwin
After doing so, certify the binary as instructed here.
Then try this for 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++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "C/C++: g++ build active file"
}
]
}

How to configure Visual Studio Code C++ compilation for custom Makefile?

I have a very simple c++ project with my own Makefile that has such commands "make a.out", "make depend" and a compiling command "make submit", plus a "make clean" which deletes the a.out and any cores in the directory.
I want to configure my VSCode so that when I press run, I would see the result of "make clean" "make a.out" followed by "./a.out". I have already installed the C/C++ extension and had my gcc installed.
These are very simple command-line commands but I just can't make the VScode work. The error says a.out is not found. In my launch.json, I have below scripts
{
// 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": "a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
What should I change the launch.json and how should I configure my task.json? Is there anything else I need to do? Thanks!

How to use the debugger in visual studio code?

I am using Visual Studio Code for C++ coding on Ubuntu 18.04.
I found that for the debugging, I should always build an a.out file and then use the debugger. If I run the code directly by ctrl+alt+n then the code runs completely but it won't give me an a.out file.
Is there any way that I can automate this process?
I have also noticed that there is no build option available.
Here is the 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": "${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"
}
]
}
You could create custom build+launch task for running your C++ code through a debugger.
Let's say we have this sample code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
vector<int> numbers{ 10, 20, 30 };
for (int n : numbers)
{
cout << n << endl;
}
}
First, make sure to have Microsoft's C++ extension installed.
Next, create a custom build task.
This tells VS Code how to compile your code.
Open the Command Palette
Select Tasks: Configure Task
Select C/C++: g++: build active file
You can other compilers that are available on your system
That would create or open an existing .vscode/tasks.json file
Configure it
{
"type": "shell",
"label": "build-my-app",
"command": "/usr/bin/g++",
"args": [
"-g",
"-std=c++11",
"${workspaceFolder}/path/to/test.cpp",
"-o",
"${workspaceFolder}/path/to/test.out"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
Set label to uniquely name your custom task
Set the compile flags like -g, -std=xxx, -l<lib>, -I<header>
Set the output filename instead of a.out
Next, create a custom launch task.
This tells VS Code how to run your code through the debugger.
Open the Command Palette
Select Debug: Open launch.json
That would create or open an existing .vscode/launch.json file
Configure it
{
"name": "run-my-app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/path/to/test.out",
"preLaunchTask": "build-my-app",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
Set name to uniquely name your launch task
Set preLaunchTask to the build task label you created earlier
Set program to the output filename you set in the build task
Finally, whenever you want to run your app, just open the Debug/Run panel and select your launch task from the dropdown (find the name you set in launch.json). Set your breakpoints then click the Play button.
You'll get the debugger controls for stepping through your code, the variables and call stack information, and the console output in the Debug Console.
Maybe you should try to compile using the the flag -g
Something like
g++ -g file.cpp
This command should generate an excutable and a directory that contains information that the debugger can read.
I am no sure if this is appropiate for VS debugger since I use gdb (gnu debugger) but maybe will work