Liero
Liero

Reputation: 27360

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

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

Upvotes: 0

Views: 6153

Answers (2)

Liero
Liero

Reputation: 27360

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

Upvotes: -2

Rohan Bari
Rohan Bari

Reputation: 7726

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:

  1. Delete tasks.json and launch.json located in .vscode folder
  2. Press F5 (debugging shortcut) again focusing on that C program file, you'll see something like:

Automatic launch configuration wizard

  1. Select your compiler which is GCC (since you're trying to debug a C program and ensure the compiler is installed into your system).
  2. You'll be prompted to select a configuration (assuming selected option: GCC), you'll get launch.json created automatically by VSCode as shown below:

Successful creation of launch.json

Note: Keep the preLaunchTask configuration in your mind (located at the bottom-most of the config).

  1. As soon as you press F5 again (this time, for creation of tasks.json) you'll get something shown below, simply select Configure Tasks:

Required error to configure tasks.json

  1. 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:

Renaming the label for debugging

And now, you can make a successful debug. A working example:

A successful example of debugging

Upvotes: 0

Related Questions