Where is my output when debugging C code in Visual Studio Code? - c

I have a simple c program, with prints something to screen.
When I debug the program I can see the DEBUG CONSOLE, however as soon as I use fgets I don't see any output. Where does my program run when debugging using VS Code?
If I just run my compiled .exe, everything is printed as expected.
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello World!\n");
printf("Enter your name\n");
char name[100];
// fgets(name, sizeof(name), stdin); // as soon as I uncomment this, no output is in the output console
printf("You name %s", name);
return 0;
}
my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/app.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}

One solution may solve your issue.
How to configure tasks.json and launch.json from VSCode
You don't need to do any type of coding for this, just follow the steps:
Delete tasks.json and launch.json located in .vscode folder
Press F5 (debugging shortcut) again focusing on that C program file, you'll see something like:
Select your compiler which is GCC (since you're trying to debug a C program and ensure the compiler is installed into your system).
You'll be prompted to select a configuration (assuming selected option: GCC), you'll get launch.json created automatically by VSCode as shown below:
Note: Keep the preLaunchTask configuration in your mind (located at the bottom-most of the config).
As soon as you press F5 again (this time, for creation of tasks.json) you'll get something shown below, simply select Configure Tasks:
Now, you'll be redirected into tasks.json, edit the label to the name you've selected in § 4 (remember that name). In other words, launch's preLaunchTask and tasks' label should be the same. Process shown below:
And now, you can make a successful debug. A working example:

Just add externalConsole": true to you configuration in launch.json.
For example:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file",
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}

Related

Compiling multiple C files inside of Visual Studio Code

How can I compile multiple C files inside of Visual Studio Code? Right now I can only compile the main file and I want to be able to compile multiple files from multiple folders and then have the ability to rename that outputted exe and have it go into a designated folder. I can do this all from the command line but it would be way easier if I could just do it from within the IDE. For example lets say I have a Main.c file and an Addresses.c file within the the default directory (next to all of the folders like .vsCode and so on) and then I have another folder named gui that also contains C files. In that example how could I compile my project so that it correctly uses all of those files then changes the outputted exe's name to test.exe (instead of it being main.exe) and places it inside of a folder labeled Builds? I have watched and read so many articles and nothing works I would really appreciate any help thank you! The following snippets are of my launch.json file and my c_cpp_properties file if you need anything else please let me know! Also I am on windows 10 also I do also have header files but I never had to specify them when compiling from my command prompt so I did not mention them above.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/MinGW/bin/gcc.exe",
"args": [
"-g",
"${fileDirname}\\**.c",
"${fileDirname}\\**.h",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
"options": {
"cwd": "C:/MinGW/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/me/Desktop/Coding/C and C++/Externel/NonKernel/Cube",
"program": "c:/Users/me/Desktop/Coding/C and C++/Externel/NonKernel/Cube/build/Debug/outDebug",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

C Program isn't Stopping at Breakpoints During Debug For Visual Studio Code

So, basically I have a breakpoint but the debugger isn't stopping at the breakpoint.
To compile I am using
gcc -g balance1.c -o ./test
The program takes in input and outputs fine it's just that I am explicitly putting a breakpoint and it isn't stopping no yellow line or anything. Granted it's the first time I've coded with C or used the debugger in any capacity so, thanks! Below 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": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/balance1.exe",
"args": [],
"stopAtEntry": false,
"justMyCode": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}

Visual studio code - debbugging stopped working [gdb, mingw, windows 10]

I searched for a solution for a week but couldn't solve this one without posting here.
I'm coding in C in Vs code and I had no issues so far (on a laptop). Few days ago debugging suddenly stopped working; this is how is goes:
simple code like:
#include <stdio.h>
int main(){
int x;
x=1;
x*=10;
printf("\ntest %d",x);
}
I press the green arrow play button to run it. I compiles and builds fine (as per task.json), but the breakpoint is not hit and also the "printf" does not get any result in the integrated terminal.
Also the "debug console" shows nothing
This is my task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe compile active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
]
}
and 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 - Compile and run debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Abilita la riformattazione per gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe compile active file"
}
]
}
I also tried and re-install everything Vs code, C/C++ Intellisense, Extention Run Code as well, unsitalled mingw and reintsalled it following the steps at "https://code.visualstudio.com/docs/cpp/config-mingw".
But the "missing debug" behaviour persisted.
On the other hand if I manually run the test.exe in the integrated terminal (powewrshell) by simply typing ".\test.exe" it work fine.
any idea?, thanks
The accepeted answer was given by olyBlackCat in the comments, which I'm quoting here to mark it as the accepted answer:
I'm seeing a lot of questions like this. Maybe try this extension instead of the default C++ one? It works for me on Linux. – HolyBlackCat Aug 16 at 17:28
Found the bug report. There's some advice there github.com/microsoft/vscode-cpptools/issues/7971

How to read console input from VScode while live-debugging

I'm trying to live-debug a C program which, using the C/C++ expansion from VScode, also allows me to write direct input to the console using scanf(). However, when I debug the code it opens the debug console, which does not allow me to enter any input.
I found two threads in which people answered this problem:
https://github.com/OmniSharp/omnisharp-vscode/issues/1053
Debug Console window cannot accept Console.ReadLine() input during debugging
Both advise to set the "console" property in the launch.json file to either "externalTerminal" or "integratedTerminal". I however couldn't find the property or create it for that matter, because it doesn't exist in my file.
I do have the "externalConsole" property, which can be set to either true or false. But both options seem to open the debug console like before.
The launch.json file:
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/teste.out",
"args": [],
"stopAtEntry": true,
"cwd": "/mnt/c/Users/David/Documents/code/puc-rio/inf1010/lab8/",
"environment": [],
"externalConsole": true,
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": [
"-c"
],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "${env:systemdrive}/",
"/usr": "C:\\Users\\David\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr"
}
}
]
What happens when I try to write something to the debug console:
Unable to perform this action because the process is running.
I had the same confusion before I saw your post. In my Windows environment, I set "externalConsole" to be true. After that, when the debugging started there will be a cmd shell of my program (not the "debug console" provided by VScode), in which stdin can be typed.!(https://i.stack.imgur.com/KKlJP.jpg)

Step-over switches thread while debugging multi-threaded C program with vscode

Stepping multi-threaded C program with VSCode's debugger switches thread on every step.
The thread where debugger jumps runs code like this:
do {
rc = nanosleep(&rqtp, &rem);
rqtp = rem;
} while (rc < 0 && errno == EINTR);
My debugger configuration is as follows:
"version": "0.2.0",
"configurations": [
{
"name": "solid Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/program",
"args": ["-a","-b"],
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
Q1:How do I get debugger to stick with the thread which ran into breakpoint?
Q2:How do I keep the focus on callstack belonging to that thread?
Answer to Q1: According to this SO thread, adding set scheduler-locking on to the gdb will let you stay in the same thread when you single-step.
Not sure about Q2, but this should give you what you need to continue. I'd think the callstack of the current thread is shown, but I'm not sure.

Resources