How to type into the integrated terminal on VS Code in Windows? - visual-studio-code

In Linux, I would be able to type into the integrated terminal no problem. I'd be able to type in user input and it would output. On Windows, I cannot do that. The output shows in the Debug Console and I cannot type into that or the integrated terminal.
In the picture, I run without debugging in C++ and when I ask for an input, it hangs there and doesn't output. I've seen CodeRunner but I rather not use that.
The picture of the terminal when running.
EDIT
{
// 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": "C/C++: g++.exe build active file"
}
]
}

By default, the Debug Console which the C++ program is outputting to does not support user input. This means that typing your input in the Debug Console will not be read by the C++ program.
To solve this problem, change the line "externalConsole": false to "externalConsole": true in your launch.json file so that your C++ program can run in an external console. This way, you can enter your user input and get interpreted by the C++ program that is being debugged.
Your launch.json should now look something like:
{
// 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, // <-- Changed to "true" in here
"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"
}
]
}
Read more here:
How to read input when debugging in C++ in Visual Studio Code?

Related

Pretty-printing not working in VS Code for C++ debugging using GDB

I am trying to use pretty-printing for debugging C++ in VS code under a Debian configuration.
Right now vectors are displayed like that : Vector display
My launch.json file is :
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Lancer",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/examples/005_friction/mode_III_slip_weakening",
"args": ["dumps", "100", "100"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/examples/005_friction/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
],
}
I have also tried to add in configurations
"gdb": {
"prettyPrinting": true}
In that case, I have an error "Property gdb is not allowed". But I have gdb installed on my computer. I also have the "C/C++" extension installed in VS code.
At last, I tried to type -exec -enable-pretty-printing in the Debug console without further success.
What should I do ?

Why does intellisense update on C debugger launch.json when it seems to break the run and debug?

Every time Intellisense says it wants to know what folder to use for debugging and I select the folder for it to work with. It updates my launch.json to the following
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "d:/programming/cs50/substitution",
"program": "d:/programming/cs50/substitution/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
I've found these settings break the running and debugging process. I can get a working result with the code below...
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": ["abc"],
"stopAtEntry": true,
"externalConsole": true,
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:/ProgramData/chocolatey/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
As far as I can see the only differences are the cwd and program properties. Is there any reason why it wants me to point the program property away from my exe file? It doesn't feel very helpful, but maybe I'm missing something in my environment that would mean the settings it suggests are beneficial.

How to use standard input redirection to a file while debugging using cppdbg in Visual Studio Code

{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": [],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Above is the launch.json file used for debugging a Fortran code. I am able to start debugging but without an input file with a Fortran namelist. What should I add here such that the debugger accepts the input file too.
Actually the executable takes in the input file as follows:
wrfchembcCT < wrfchembc_namelist
But I am not able to debug while passing the data file to the Fortran code.
all arguments with redirection symbols <>| are quoted, so redirection will not work in VSC
Option is to add a CLI argument to the application that will open the file in the argument as stdin
{
"version": "0.2.0",
"configurations": [
{
"name": "Run GDB",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/wrfchembcCT",
"args": ["--stdin", "wrfchembc_namelist"],
"stopAtEntry": false,
"cwd": "/mnt/e/Fortran_Codes/wrfchembc_CT/wrfchembc_CT_pkg/",
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
}
]
}
Or you can start the program with a task, maybe starting a shell script that has redirection. And attach the debugger to this running program.
I got this solved by providing complete path to the executable in program section. Its now working for some reason.

How do I make vs code put the output of my c program in TERMINAL panel?

I'm trying to build and run C code with vscode on windows 10.
I've gone through the vscode doc for mingw configuration, followed the steps there and managed to run a .c file with vscode.
However, there's still an issue yet.
each time run my program via "Run | Run Without Debugging", the panel switches automatically to "TERMINAL"
So I have to switch to the DEBUG CONSOLE manually each time I run the code, which is tediously boring.
Is there a way to keep the "DEBUG CONSOLE" panel active or show the output of my program in "TERMINAL" panel
I also tried the suggestion in another stackoverflow post, but it doesn't work for me.
tasks.json
Here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "E:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: E:\\MinGW\\bin\\gcc.exe"
}
]
}
launch.json
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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\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"
}
]
}
How do I make vs code put the output of my c program in TERMINAL panel?
You can use Code Runner with some simple configuration.
Install Code Runner.
Type Ctrl + Shift + P
Search and open Open Settings(JSON)
Add the following json snippets to your settings.json:
"code-runner.runInTerminal": true
Every time you want to run your c code, just type the icon from the upper right corner that Code Runner provide.
For step 4, you can also open vscode settings and change it on GUI.

vscode cpp build tasks.json type:"cppbuild" error

VSCode : 1.61.0
Linux: Ubuntu 20.04.3 LTS
I'm trying to build c++ program using by VSCode.
when the run build, VSCode showing this message.
I know build tasking need matching launch.json file's preLaunchTask with tasks.json file's label. and I'm already set it.
tasks.json
{
"tasks": [
{
"type": "cppbuild", // but "shell" is working
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"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++ - 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"
}
]
}
The problem is, if I change the "type" value in tasks.json, from shell to cppbuild, It doesn't work and show the error message like to.
showing error message when the run build
what is different "cppbuild" and "shell"? and What should I do how using "cppbuild" value?
Te values of cppbuild are fine. Try changing in launch.json the value:
"preLaunchTask": "C/C++: g++ build active file",
to:
"preLaunchTask": "cppbuild",
The difference between shell and cppbuild is that the first open the BASH and runs a command you write, the second, cppbuild, runs an specific binary (or program).