Previously, when I tried to debug in VSCode on Windows 10 using the C/C++ extension and MinGW32's g++ and gdb, I was able to press F5(the default "start debugging" hotkey), set up my tasks.json and launch.json, and the program's output would appear in the integrated terminal, as well as any prompts for input. This was useful especially when I needed to also provide input to the program while debugging it for schoolwork, without opening an external shell. However, this is no longer the case and I'm confused as to why, as I haven't actively changed anything to cause this to occur, and now all program output is appearing in the Debug Console, where I cannot enter input. I'd prefer to restore things to what I've described above, where all output/input occurs in the integrated terminal, but I'm unsure how I would accomplish this. I have tried to debug Python programs in VSCode as well, using the available Python extension, but the output of print statements appears in the integrated terminal, where I'd expect it to. In addition, the Code Runner extension works with my current issue, but I'd prefer to restore my working environment to its' previous state. My current version of VSCode is 1.49.2, my C/C++ extension version is 1.0.1, and my Python extension version is 2020.9.111407. I am also using g++.exe (MinGW.org GCC Build-20200227-1) 9.2.0 and GNU gdb (GDB) 7.6.1
For maximum clarity, compiling and debugging from the integrated terminal by manually typing the g++ and gdb commands works fine, but F5 no longer produces the behavior I'd expect.
I've made sure that my launch.json has "externalConsole": false set properly, my Terminal: Explorer Kind setting is set to "integrated", and Terminal > Integrated: Inherit Env is set to true. I've tried to toggle all of these options, running in Administrator mode, running in compatibility mode for Windows 8, rolling back to old versions of the extensions I'm using, and nothing has changed this behavior.
tasks.json:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Programming\\MinGW32\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.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": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Programming\\MinGW32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
test.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello\n";
system("pause");
return 0;
}
This is what happens when I press F5 on a python file, this is the kind of behavior I'd expect.
This is what happens when I press F5 on my cpp file, no output appears in the integrated terminal.
Instead, it appears here.
I have chosen to omit the code from my .py file in the first image due to its' simplicity
UPDATE(Sept. 28, 2020): Apparently this issue has been documented here, and the solution that worked for me was to install mingw-w64 from their sourceforge, then update my mingw path in system environment variables.
The only solution I found is to set "externalConsole": true, in launch.json, working in the external console instead.
Related
I've installed WSL2 and build tool and everything is working well on linux console (including a test c++ program I was able to debug with dbg).
I'm now trying to have a working development environment on my windows10 machine, by installing VSCode and a couple of extensions (c/c++ and Remote WSL).
Build works just fine, but when I try to debug, after executing (F10) a couple of lines I get the error:
Unable to open 'libc-start.c': Unable to read file 'vscode-remote://wsl+ubuntu-20.04/build/glibc-YYA7BZ/glibc-2.31/csu/libc-start.c' (Error: Unable to resolve non-existing file 'vscode-remote://wsl+ubuntu-20.04/build/glibc-YYA7BZ/glibc-2.31/csu/libc-start.c').
From that point on debugger basically doesn't work anymore, every time I press F10 (or F11) I get a new popup with the same error
screenshot of error and dev environment
Pressing the "Create File" button results in an "Unable to write file 'vscode-remote://wsl+ubuntu-20.04...." (same file as above).
here following 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
and my tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C++ Compile",
"command": "/usr/bin/g++",
"args": ["-g","*.cpp","-o","${fileDirname}/${fileBasenameNoExtension}"],
"options": {"cwd": "${workspaceFolder}"},
"problemMatcher": ["$gcc"],
"group": {"kind": "build","isDefault": true}
}
]
}
I've searched quite a bit before posting. Other have had similar problems in older posts but most of them claimed that being an old bug and they say the problem was solved with the upgrade. I'm running latest version of everything I'm using (including VSCode and the plugins).
thank you in advance to anyone helping out.
Adding
"sourceFileMap": { "//wsl$/Ubuntu-20.04" : "/" }
to the launch.json resolved the problem for me.
I'm at a bit of a loss here, I hadn't really expected this to be difficult. I usually work on Linux, but today I had some work that I needed to do and only had a Windows machine. I thought this would be no problem, I can install git for windows, clone my project, and get right to work. Its just been a huge mess. I'm really hoping someone can help me understand where I went wrong in setting all this up on Windows. It isn't something I plan to do frequently, but definitely something I want to be able to do on a Windows machine in a reasonable amount of time.
I'm using WSL and have set my default VSCode Windows integrated terminal to C:\WINDOWS\System32\bash.exe
I installed Windows 10 SDK to fix crtdbg.h include errors as a dependency against <iostream>
I installed gdb with MinGW -
I set the path environment variable
I created a 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) CDLL Driver",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/driver",
"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
}
]
}
]
}
My MinGW bin contains the following
I launch my debug task in VSCode and I get the following error
cmd /C "c:\Users\shaun\.vscode\extensions\ms-vscode.cpptools-0.28.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-4n4ohh2f.ibt --stdout=Microsoft-MIEngine-Out-1irudlfy.q5x --stderr=Microsoft-MIEngine-Error-fg20cagk.ynl --pid=Microsoft-MIEngine-Pid-kzdzn4p4.lro --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "
Command 'cmd' not found, but there are 16 similar ones.
I can provide more information if needed. I'm really hoping I missed something simple here that would be obvious to someone who works on Windows.. Thank you in advance, I really appreciate the help!
If you are using WSL to compile the project you should not use MinGW gdb.
You need to install gdb on you Linux subsystem (using native tools like apt if you are using Ubuntu WSL), reopen your project in WSL and configure the WSL path to gdb.
I was able to successfully debug using this setup on WSL.
Replace your launch.json file with this file
{
"version": "0.2.0",
"configurations": [
{
"args": ["1"],
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"stopAtEntry": false,
"cwd": "${fileDirname}",
"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++: gcc.exe build active file"
}
]
}
Make Sure you have installed MinGw Compiler and gdb debugger
I was following the documentation on https://code.visualstudio.com/docs/cpp/config-mingw to set up my C++ development environment on VS code.
I succesfully created my build task and ran my build task, but when i tried to set up my debugger (GDB debugger), i got the following output on the terminal.
C:\Users\Ayon\c++\helloworld> cmd /C "c:\Users\Ayon\.vscode\extensions\ms-vscode.cpptools-0.28.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-shlwrnf5.x12 --stdout=Microsoft-MIEngine-Out-vg12hskh.c52 --stderr=Microsoft-MIEngine-Error-nrcvh0zz.0u0 --pid=Microsoft-MIEngine-Pid-m1nmxyvs.qk2 --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "
'cmd' is not recognized as an internal or external command,
operable program or batch file.
Im curious as to how the "cmd /" came about in the output. Please help me fix this error.
Thanks in advance.
For reference, 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": "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\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
Did you add mingw64/bin to your system path? Here's how to do so:
Control panel -> Edit path -> Environment variables -> Path -> New
You can also find more guidance on
vscode official website.
I believe the problem is with the MinGW installation. I also faced this problem for a long time, solved it by doing the following:
Completely removing the current MinGW Video tutorial
Installing MinGW-w64 from the site mentioned in vscode documentation. installation video tutorial
After this, I followed the configuration mentioned in this video. The video is for c code, but you can make the required changes to c++ code. Now I'm able to debug in vscode.
By this way, you will also be able to use the latest versions of gdb and g++ available.
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
Good evening to everyone, I try to debug this little program in visual studio code in ubuntu:
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
std::string folder = argv[1];
}
but the debug terminate with this error in the terminal:
"terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
"
and this in the debug console:
"Unable to open 'raise.c': Unable to read file (Error: File not found (/build/glibc-4WA41p/glibc-2.30/sysdeps/unix/sysv/linux/raise.c))."
So the question are:
1) Is possible to display the number of the line where the error occurs? (In this case line 6)
2) Why does this error happen, and how to avoid it?
3) For avoiding this problem I can write, for example:
string folder = "/home/lorenzo/Images";
but I don't want to do that. For "running" the program from the terminal I write ./main /home/lorenzo/Images, so I pass the folder to the program in this way. Is possible to do the same thing when debbuging, without writing the folder directly in the program, or using cin?
Thanks in advance!
If you want to debug with VS Code, there's a setup that you have to do for each project, but after the setup is complete, it's straightforward.
Install gdb if you haven't already. You then need to choose a configuration in the debug panel. Instructions can be found here. Compile your program with -g at a minimum. I prefer also adding in -O0 to minimize optimizations.
Once you get set up, you're now ready to debug with VS Code. Now, to [hopefully] answer your questions.
gdb can do this for some segmentation faults; generally you'll want to learn how to move through the code yourself.
I attempted to compile and run your program, and it worked just fine. Is the name of your executable main? I compiled on Debian using gcc 5.5. I didn't name my executable, so my invocation looked like this:
./a.out /home/sweenish/tmp. Since mine didn't fail, I can't offer much help here. But your compiler is saying that a file doesn't exist. Did you install the build-essential package?
Yes, you can automate the extra argument by adding the option to your launch.json file for the VS Code project.
Here's a short example:
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
std::string folder = argv[1];
std::cout << folder << '\n'; // Set a breakpoint here
}
I added an extra line of code to your example. Set a breakpoint by clicking left of the line number, a red circle will appear.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-Wall",
"-std=c++17",
"-g",
"-O0",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/home/linuxbrew/.linuxbrew/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
This is a slightly modified auto-generated task. I added -Wall, -std=c++17, and -O0. The file is tasks.json. If you don't create it before attempting to execute the debug, it will ask prompt you to generate it.
{
// 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": [
"/home/lorenzo/Images"
],
"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": "gdb"
}
]
}
This is the auto-generated launch.json. Notice that I added the path argument. The debugger will always invoke with that argument, saving you some typing.
I then hit the Play button in the debugging panel while my C++ file is active, and it will compile and start the debugger for me. From the debug console, running:
-exec print argv[1] prints the file path that I am using as an argument to the program.