Answer a question

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.

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.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐