6

Currently my C++ program runs embedded in the VS Code window, right at the bottom panel. How can I run it in an individual console window as it is in VS?

I tried to turn the "settings/Terminal/Explorer" option "Kind" from "integrated" to "External" but it was no good.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Fifnmar
  • 372
  • 1
  • 5
  • 9
  • You mean just by running the project from within using VSC using F5? – Vincent Oct 12 '19 at 10:42
  • 2
    This has nothing to do with C++, still you shouldn't be using VIsual Studio Code for C++ if you're on Windows. You should be using Visual Studio IDE. In Visual Studio, just launching the application will execute in a seperate terminal window. – Irelia Oct 12 '19 at 10:58
  • @Nina Thanks, I have installed both VS and VSC on my PC. It bores me to create a project for each case. I practice with many little cases, you know, because I am new to coding... – Fifnmar Oct 13 '19 at 10:51
  • I had the same problem, here's the solution I found: https://stackoverflow.com/questions/58221544/how-to-make-code-runner-run-in-external-terminal-command-prompt Just add those code lines into your settings.json file :) – Fernando Assef Jun 17 '21 at 18:46

3 Answers3

8

You can create a launch configuration that runs your app in your OS's native terminal/console.

For example I have this very simple test file:

#include <iostream>
int main (void)
{
    int num;
    std::cout << "Enter number: " << std::endl;
    std::cin >> num;
    std::cout << num << std::endl;
}

1st, install Microsoft's C/C++ VS Code extension to add support for debugging C++ files.

2nd, create a build task. Open the command palette, find Tasks: Configure Tasks then select a suitable C++ compiler (ex. g++ in my case). If this is the first time you are doing this, VS Code is going to create a .vscode/tasks.json folder in your workspace with a default task. Configure it to build your app, like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build-test",
            "type": "shell",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${workspaceFolder}/app/test.cpp",
                "-o",
                "${workspaceFolder}/app/test"
            ]
        }
    ],
}

3rd, create the launch task. Open the Debug panel. If you are doing this for the first time and you have no existing launch configurations, just click on the create a launch.json file link:

VS Code - Debug Panel - no existing configurations

If you already have existing configurations, open the dropdown and select Add Config.

VS Code - Debug Panel - dropdown with existing configurations

It should open up the existing launch.json file and show you a popup of which type of launch configuration to use. Select C++ with Launch

VS Code - launch.json - C/C++ option

Update the configuration like this:

{
    // 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": "run-test",
            "type": "cppdbg",
            "request": "launch",
            "preLaunchTask": "build-test",
            "program": "${workspaceFolder}/app/test",
            "cwd": "${workspaceFolder}",
            "externalConsole": true,
            "args": [],
            "environment": [],
            "stopAtEntry": true,
            "MIMode": "lldb"
        }
    ]
}

The important configs here are "preLaunchTask": "..." and "externalConsole": true. The preLaunchTask should be set to the build task set earlier. The externalConsole, if set to false it opens it in the Integrated Console. Since you don't want to run it in the Integrated Console, set it to true.

Now, whenever you want to run your app, just open the Debug panel, then run your launch task (same name as the name you set in the launch.json). Note that in the launch.json config, I set stopAtEntry to true, to give me a chance to see the external console window and then provide input to the prompt. You can remove it if you don't need it.

enter image description here

enter image description here

If all goes well, it's going to run it by launching an external console.

For more information, the complete guide to setting this up is in VS Code's Configuring C/C++ debugging docs.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Thanks, I have found where the problem is. – Fifnmar Oct 16 '19 at 10:22
  • No property called `externalConsole`, had to use `"console": externalTerminal`, however that gives an error related to debugging and doesn't pass the parameters to the program on start – rboy Jan 27 '22 at 16:46
  • @rboy There _is_ an `externalConsole`: https://code.visualstudio.com/docs/cpp/launch-json-reference#_externalconsole. As I mentioned in my answer, make sure to install [Microsoft's C/C++ extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools), make sure you are editing the launch.json file, and make sure the launch config is of `"type": "cppdbg"`. Just checked it now with VS Code 1.63.2 and C/C++ extension v1.1.0 and [I have `externalConsole`](https://i.stack.imgur.com/MJDqX.png). – Gino Mempin Jan 27 '22 at 23:27
  • Okay I got it to work for a random external console program. Remove `MIMode`, set `"type": "cppvsdbg"` and instead of `externalConsole` use `"console": "externalTerminal"` and `"stopAtEntry": false` – rboy Feb 02 '22 at 23:19
  • @rboy You could post that as a separate answer since that's complete different from I posted on mine. So that others can have an alternative if my solution doesn't work for them. – Gino Mempin Feb 02 '22 at 23:51
2

VS Code for Windows

This is a modified version of the answer by @gino-mempin which allows you to launch an external program from VSCode without having VSCode trying to attach to the launched program (thinking it's a debugger) which could lead to an error after launch. You can optionally configure the launch task to run build tasks prior to launching the external program.

Additional references https://code.visualstudio.com/docs/cpp/launch-json-reference

You FIRST need to install Microsoft's C/C++ VS Code extension before using this launch configuration.

Then configure your launch.json file like this (uncomment any optional parameters which you may need):

{
    "configurations": [
        {
            "name": "Start External Program",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "< the path to your exe to launch >",
            "console": "externalTerminal",
            //"cwd": "optional working directory",
            //"args" : [ "optional arguments", "each argument is enclosed in quotes", "separated by commas" ],
            //"preLaunchTask": "optional name of pre launch build task",
        }
    ]
}
rboy
  • 2,018
  • 1
  • 23
  • 35
-2

Actually pretty easy. Open a new window of VS code and open terminal. And copy paste exact command of compiling and running the C++ program. So in this was you have 1 window for code browsing and another from program execution.

Vinod Kumar
  • 562
  • 5
  • 12