Can't receive input (cin) in c++ program in Windows vscode - c++

I have been trying to figure out how to receive input in Windows vscode c++ program for example using cin. I have already tried running with MingW and cl and both have the same result.
Terminal output is being sent to vscode debugconsole and I can't interact with the program.
The same program runs fine if it is run directly via terminal or cmd command prompt.
Here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "cl.exe build active file",
"type": "shell",
"command": "c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
And here is my launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "cl.exe build active file"
}
]
}
I've seen other suggestions like using "code-runner.runInTerminal": true and "terminal": "integrated" but this has not worked for me. I have already seen this other post: Visual Studio Code: Take Input From User
But so far no joy.
Here's a screen of vscode that shows the issue with a small c++ program:
You will see from above that the output is directed to the debugconsole pane and no interaction is possible from there.
Whereas, the advice from https://code.visualstudio.com/docs/cpp/config-mingw indicates the IO should go to the terminal pane.

Related

Use integrated terminal for debugging 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?

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

dbg not working on VScode with WSL2 - Unable to resolve non-existing file 'vscode-remote://wsl+ubuntu-20.04..'

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.

Compile AND run c++ from VS Code (macOS)

I'm trying out c++ for the first time - using VS Code, as I like that text editor.
I've followed this tutorial to set it up: https://code.visualstudio.com/docs/cpp/config-clang-mac
I have got the following tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"helloworld.cpp",
"-o",
"helloworld.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The compilation works fine and I can run the .out-file from the terminal. But I would like to reduce those 2 steps to 1 (compile AND run). Is that possible?
I've heard of the Code Runner extension, but would like to try to set it up in the tasks-file - also to learn a little on how it works.
Anyways - Any hints and suggestions are most welcome! I'll post here if I figure it out on my own!
What you need to do is configure a launch task in your launch.json and add your build task to the prelaunch tasks. Mine looks like this (I use this on Windows with minGW, but on macOsX it should work the same). This launches your program with your debugger and executes your build task first. You don't need any extensions apart from the Microsoft C++ one to run and even have full debug support with breakpoints, etc.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "Path to your executable",
"args": [],
"stopAtEntry": false,
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "Path to your debugger",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Name of your Build Task"
}
]
}

How to configure Visual Studio Code for C++ on Windows?

I face problem with configuring launch.json file on my Windows and it shows error message
Debug adapter process has terminated unexpectedly.
I have set up "MinGW" and configured g++ compiler, and now Visual Studio Code compiles correctly. When I press Ctrl+Shift+B, it creates a.exe file in project folder.
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (Windows)",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false
},
{
"name": "C++ Attach (Windows)",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command.pickProcess}"
}
]
}
My task.json file:
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"showOutput": "always",
"args": ["-std=c++11","-g", "main.cpp"]
}
It may be wrong, but I suppose you need to compile your .c/.cpp file with microsoft cl.exe compiler (not by gcc) to use microsoft debugger on it. To setup paths for cl, call the vcvarsall.bat with parameter x86 or x64 (depends on what you need) and then cast cl to your source file. By the way check this article (the part with json configuration exactly) to ensure, where you did wrong https://www.40tude.fr/blog/how-to-compile-cpp-code-with-vscode-cl/