您可以创建一个launch configuration,在您操作系统的本机终端/控制台中运行您的应用程序。
例如我有这个非常简单的测试文件:
#include <iostream>
int main (void)
{
int num;
std::cout << "Enter number: " << std::endl;
std::cin >> num;
std::cout << num << std::endl;
}
1st,安装 Microsoft's C/C++ VS Code extension 以添加对调试 C++ 文件的支持。完整的设置指南在 VS Code 的 Configuring C/C++ debugging 文档中。
第二,创建一个构建任务。打开命令面板,找到 Tasks: Configure Tasks 然后选择合适的 C++ 编译器(例如我的例子中的 g++)。如果这是您第一次这样做,VS Code 将在您的工作区中创建一个 .vscode/tasks.json 文件夹,其中包含默认的构建任务。配置它以构建您的应用程序,如下所示:
{
"version": "2.0.0",
"tasks": [
{
"label": "build-test",
"type": "shell",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/app/test.cpp",
"-o",
"${workspaceFolder}/app/test"
]
}
],
}
3、创建启动任务。打开 Debug 面板(从右侧边栏),单击下拉菜单,选择 Add Config,然后选择 C++。同样,如果您是第一次这样做,VS Code 将在您的工作区中创建一个 .vscode/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": "run-test",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "build-test",
"program": "${workspaceFolder}/app/test",
"cwd": "${workspaceFolder}",
"externalConsole": true,
"args": [],
"environment": [],
"stopAtEntry": true,
"MIMode": "lldb"
}
]
}
这里重要的配置是"preLaunchTask": "..." 和"externalConsole": true。 preLaunchTask 应该设置为之前设置的构建任务。 externalConsole,如果设置为 false,它将在集成控制台中打开它。由于您不想在集成控制台中运行它,请将其设置为true。
现在,只要您想运行您的应用程序,只需转到“调试”面板,然后运行您的启动任务(与您在 launch.json 中设置的 name 同名)。请注意,在 launch.json 配置中,我将 stopAtEntry 设置为 true,让我有机会查看外部控制台窗口,然后为提示提供输入。如果不需要,可以将其删除。
如果一切顺利,它将通过启动外部控制台来运行它。
同样,有关详细信息,请参阅 VS Code 的 Configuring C/C++ debugging 文档。