Visual Studio Code Integrated Terminal not Displaying Text - c++

I'm brand new to MacOS and I am attempting to set up a programming environment, my IDE of choice being Visual Studio Code. When the program runs it, by default, prints in output. However, output crashes when asked to gather input. The online solution the I found for that was to output the code through the terminal, yet now nothing is displayed in the terminal.
I'm posting this here instead of a bug report as I'm unsure whether the fault is mine or the program's.
Here is the simple code I am attempting to run:
#include <iostream>
int main()
{
int i;
std::cout << "Enter a number: ";
std::cin >> i;
std::cout << "\n" << i;
return 0;
}
When run through output, it will display the first part, then crash when input is requested. When run through the terminal, the terminal only displays: "cd "(directory location)" && g++ main.cpp -o main && "(directory location)"main" and nothing else.
Below are my tasks.json and launch.json:
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "c++ test program",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
The only setting that has been changed would be "code-runner.runInTerminal" which has been set to true.

This is true when using Code Runner
so the solution is:
use Code Runner and then press Ctrl+, to edit settings then search for
code-runner.runInTerminal and set code-runner.runInTerminal to true, like so:
{
"code-runner.runInTerminal": true
}
this works fine for me.
I hope this helps.

Related

Unable to initialize vector with initializer list in visual studio code

#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> v = {1,2,3,4};
for (int x : v)
{
cout << x << ' ';
}
return 0;
}
When I run the above code in vscode I receive the following ERROR:
non-aggregate type 'vector' cannot be initialized with an initializer list gcc [7, 17]
NOTICE - the error includes gcc even though that is not the compiler I am using.
The code compiles fine in the terminal and in Xcode so I know it has something to do with vscode. How do I fix this issue?
NOTE - I am using I C/C++ IntelliSense with the following configurations: Compiler Path (/usr/bin/clang++) IntelliSense mode (macros-clang-arm64) Include path (${workspaceFolder}/**) C standard (c17) C++ standard (C++17).
I copied your code and named it test.cpp. I faced the same issue and I solved it by adding some configurations. Find tasks.json and add something in args.
"args": [
"-g",
"-Wall",
"-std=c++11",
"test.cpp"
]
It works on my MAC! I used command+shift+B to compile and it generated a.out after compiling. Then you can run it by F5. I also post my launch.json here, where /Users/work/Foo is my workspaceFolder. Pay attention to the line of program, I have changed this line. Good luck!
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"cwd": "/Users/work/Foo",
"environment": [],
"program": "/Users/work/Foo/a.out",
"MIMode": "lldb",
"externalConsole": true
}
]
}
You might post your code and how you're compiling it. The following worked for me:
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1,2,3,4};
for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
std::cout << *i << ' ';
}
std::cout << std::endl;
return 0;
}
Compiled and run like so:
$ g++ -std=c++11 test.cpp
$ ./a.out
1 2 3 4
$
Have you gone through this official tutorial thoroughly?
Configure VS Code for clang on macOS
Most probably you have have not configured your tasks.json correctly. Your build system is not using the right c++ standard.
Can you post your tasks.json so that we can understand your configuration exactly.
Here is the tasks.json file config from documentation:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Note: I don't have reputation to comment on question, so asking you here.

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

how to use these debugging flags in my C++ code on VS code (Windows 10)

Code (if required) >>>
// two pointer
#include <iostream>
#include <string>
using namespace std;
int main () {
int arr[] = {3, 5, 7, 9, 13, 15, 16} ;
int sum = 40 ;
int i =0;
int j = *(&arr + 1) - arr - 1 ;
bool found = false ;
cout<<j<<endl;
while ( i<j && found == false) {
if (arr[i] + arr[j] == sum){
found = true ;
}
else {
if ( arr[i] + arr[j] < sum){
i++ ;
}
else {
j-- ;
}
}
} // end of while
if (found == true)
cout<<arr[i]<<" "<<arr[j]<<endl;
return 0 ;
}
Problem in brief - I cannot compile my small piece of code with the some debugging flags in VS Code using cmd terminal, the debugging flags are : -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
Details -
my machine - WIndows 10 64 bit
Compiler - GCC C++ compiler (g++) and GDB debugger from mingw-w64
IDE - VS Code
file name - main.cpp
So basically i m new to VS CODE OR any IDE environment. I followed the steps from here https://code.visualstudio.com/docs/cpp/config-mingw, to set up my VSCode for build task, debugging main.cpp .VS code configurations, task.json file and launch.json file.
BY compling and running main.cpp through cmd terminal in VS Code using these commands
C:\Users\Humble Fool\Desktop\dummy>g++ main.cpp
C:\Users\Humble Fool\Desktop\dummy>.\a.exe
(my code gets executed properly as show below)
enter image description here
But when i went on to compile with these debugging flags, -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv through the following command in terminal :
C:\Users\Humble Fool\Desktop\dummy>g++ main.cpp -g -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
i got this error message :
C:\Users\Humble Fool\Desktop\dummy>g++ main.cpp -g -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lubsan
collect2.exe: error: ld returned 1 exit status
So can anyone guide me with a step-by-step guide to compile with those debugging flags ?
If it can be fixed using tasks.json or launch.json file, then will i have to update these two files for every folder that I import in VS Code ?
I m also attaching my tasks.json and launch.json files
tasks.json file -
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
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++.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-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\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'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

It's no any response after starting debugging

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