I found this issue, but it was closed https://github.com/microsoft/vscode/issues/112165
The problem is that VSCode can not read data from file when debugging c++ code.
Steps to Reproduce:
create a .cpp file
create a .txt with some data
use freopen or fopen, read data from the .txt file
debugging
the variables never change its value
Screenshots:
The value of T was supposed to be 3 (which it is under regular execution) but under debugging it is a random value 16.
Everything is default
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": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-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"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
}
]
}
I use
#ifdef SHERAJ
freopen("input.txt" , "r", stdin);
#endif
for competitive programming and VS Code on Mac debugger runs this perfectly fine. Please let me know a fix for this because I recently switched from Mac to Windows.
Okay, I get what is happening here. In the launch.json file, the "cwd" value is the path of the current working directory of the process being launched or debugged, this is where the input files are read from and output files and written to by default.
You should set it to the directory where the files you are working with are located.
Answer from here. Similar question: freopen() does not read / write to existing file, niether creates new file in vscode
Related
I am new to cpp programming, and I am using Visual Studio Code as my IDE/editor. I created a custom build task which builds the current active file (file open in editor that I am working on). Below is my taks.json.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-10 build active file",
"command": "/usr/local/bin/g++-10",
"args": [
//"-g", // include to add debugging support
"-O2",
"${file}",
"-o",
//"${fileDirname}/${fileBasenameNoExtension}"
"build/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/bin/g++-10"
},
{
"type": "cppbuild",
"label": "C/C++: g++-10 run active file",
"command": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "none",
"detail": "compiler: /usr/local/bin/g++-10"
}
]
}
and the 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++-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
// "preLaunchTask": "C/C++: g++-10 build active file"
}
]
}
I am able to run the build task to generate cpp object file. But how do I run the file just built from within IDE. I essentially just want to emulate build and run for quickly checking programs. I tried creating a new task (the second item in tasks.json's "tasks"), but when run it does not works. I tried finding online but to no avail.
Currently I build the file using this tasks and then switch to terminal and run the file. This workflow is ok, but had this been achieved, I would have been able to do build and run as supported in common IDEs. Is there a way to achieve this?
Thanks!
Attaching screen snips for F5 debugging issue.
To read from standard input using a debugging session, standard input must be redirected from a text file.
To redirect standard input, you need a command like this
program_name < input.txt
So you have to add these commands to you launch
"args": [
"<",
"input.txt"
],
In launch.json change
"externalConsole": false,
to
"externalConsole": true,
I used to use Visual Studio 2019 for developing C and C++ programs, now I have moved to VS Code, and I find that I can't open a file with a relative path when debugging, only with absolute path it can work.
here's my launch.json and tasks.json files:
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": "C:\\mingw64\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Thank you so much for solving my problem.
Relative paths are encountered from the current working directory of the application, and you've provided
"cwd": "C:\\mingw64\\bin",
if you want to run from the current project root directory set it to like this (or set it to where your prefer)
"cwd": "${workspaceRoot}"
Edit: if you set it in that way to be able to run gcc consider adding C:\\mingw64\\bin in your PATH variable.
I'm getting an issue where I can build and run a project with c++17 flags enabled, but I can't actually debug it. The debugger launches in vsCode, but all the c++ 17 features are red squiggled, and those lines never execute properly. I'm guessing it's a problem with my launch.json, but I can't figure out what it is that I'm missing...
I'm running windows 10, and here's the relevant configuration in vsCode:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-std=c++17",
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task 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++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": ["-std=c++17"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-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"
}
]
}
Welp - not much of an answer, but the issue isn't happening when I just use cl.exe as the compiler instead of gcc. I've since just stopped using gcc for this project after trying to get it to work for about an hour.
https://code.visualstudio.com/docs/cpp/config-msvc
I am trying to learn C++, but I am unable to get any code running.
#include <iostream>
int main()
{
std::cout << "Hello world!";
return 0;
}
When I run this(or another file), it creates a different unrecognized file. (https://imgur.com/a/07rpngl, picture 2) Then it gives the error at picture 1.
This is the 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": "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": "cpp build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
And this is the task file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
The settings.json file is empty.
Both running without debugging and with it gives the same error. How can I fix this?
Ways that were tried:
1) Tried restarting both the application and the computer, doesn't work.
2) Tried using terminal to compile, run and debug the code. It works, but is there a way to do this over VS Code? (Should I open another question just for VS Code?)
# First, change your directory to the one that you want to use
# Run the following to compile in the same directory:
g++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -g -o nameforexe source.cpp
# Change -std=c++11 to whatever version of c++ you want to use, and also the file names.
# To run it after this normally do this:
./nameforexe
# To debug, do:
gdb ./nameforexe
# After gdb opens, write "run" without " to run the file.
# If there is a problem, it will tell you. If there isn't it will run normally.
Thanks to David C. Rankin for this. There is more info in the comments for this question.
3) Using CMake(suggested by dboy). I wasn't able to do this. After it says generated in the output section, I wasn't able to find the file to run the HelloWorld application. (Image 3 shows the files)
The CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10.2)
project("HelloWorld")
set(SOURCE Helloworld.cpp)
add_executable("HelloWorldBuild" HelloWorld.cpp)
It didn't show any problems while compiling. CMake might be to advanced for me for now.
When I run the debug, the output from my program isn't displayed on the integrated terminal.
This actually used to work a few days ago. I haven't changed any config files and yet, perhaps due to an update, this has changed.
Note that there is no "console" option with lldb.
This also creates a major issue that I can't provide input to my running program.
This is what I see in the integrated terminal:
[1] + Done "/usr/bin/lldb-mi" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-871ntlpp.93i" 1>"/tmp/Microsoft-MIEngine-Out-msz8v24g.agq"
And that's it. The case is the same when using "externalConsole":true.
How do I keep whatever output is currently in the debug terminal inside it, but also see the normal program output and be able to give input in the integrated terminal?
Also, this behavior leads me to think, currently, what is even the point of having the integrated terminal? Nothing is happening there.
here are my config files:
"version": "0.2.0",
"configurations": [
{
"name": "clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "clang++ build active file",
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
unfortunately
https://code.visualstudio.com/docs/cpp/launch-json-reference
macOS: When set to true, it will spawn an external console through lldb-mi. When set to false, the output can be seen in VS Code's debugConsole. Due to limitations within lldb-mi, integratedTerminal support is not available.