How to run program in a pop-out console window using VS Code?

Viewed 13589

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.

3 Answers

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.

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

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.

Related