如何使用 VS Code 运行 在弹出式控制台 window 中编程?
How to run program in a pop-out console window using VS Code?
目前我的 C++ 程序 运行 嵌入在 VS Code window 中,就在底部面板中。我如何在单个控制台中 运行 它 window 就像在 VS 中一样?
我尝试将 "settings/Terminal/Explorer" 选项 "Kind" 从 "integrated" 更改为 "External" 但效果不佳。
您可以在 OS 的原生 terminal/console.
中创建 launch configuration 您的应用 运行
例如我有这个非常简单的测试文件:
#include <iostream>
int main (void)
{
int num;
std::cout << "Enter number: " << std::endl;
std::cin >> num;
std::cout << num << std::endl;
}
1、安装Microsoft's C/C++ VS Code extension以添加对调试 C++ 文件的支持。
2、创建构建任务。打开命令选项板,找到 Tasks: Configure Tasks 然后 select 一个合适的 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、创建启动任务。打开调试面板。如果您是第一次这样做并且没有现有的启动配置,只需单击 创建 launch.json 文件 link:
如果您已有配置,请打开下拉菜单并select 添加配置。
它应该会打开现有的 launch.json 文件并向您显示要使用的启动配置类型的弹出窗口。 Select C++ 与 Launch
像这样更新配置:
{
// 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)。请注意,在 launch.json 配置中,我将 stopAtEntry
设置为 true
,让我有机会看到外部控制台 window然后向提示提供输入。如果你不需要它,你可以删除它。
如果一切顺利,它将通过启动外部控制台运行。
有关详细信息,请参阅 VS Code 的 Configuring C/C++ debugging 文档中的完整设置指南。
其实很简单。打开一个新的 window 的 VS 代码并打开终端。并复制粘贴编译和 运行 C++ 程序的确切命令。因此,您有 1 个 window 用于代码浏览,另一个用于程序执行。
Windows
的 VS 代码
这是 答案的修改版本,它允许您从 VSCode 启动外部程序而无需 有 VSCode 试图附加到已启动的程序(认为它是调试器),这可能会导致启动后出错。您可以选择将启动任务配置为 运行 在启动外部程序之前构建任务。
其他参考文献https://code.visualstudio.com/docs/cpp/launch-json-reference
您首先需要安装 Microsoft's C/C++ VS Code extension,然后才能使用此启动配置。
然后像这样配置您的 launch.json
文件(取消注释您可能需要的任何可选参数):
{
"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",
}
]
}
目前我的 C++ 程序 运行 嵌入在 VS Code window 中,就在底部面板中。我如何在单个控制台中 运行 它 window 就像在 VS 中一样?
我尝试将 "settings/Terminal/Explorer" 选项 "Kind" 从 "integrated" 更改为 "External" 但效果不佳。
您可以在 OS 的原生 terminal/console.
中创建 launch configuration 您的应用 运行例如我有这个非常简单的测试文件:
#include <iostream>
int main (void)
{
int num;
std::cout << "Enter number: " << std::endl;
std::cin >> num;
std::cout << num << std::endl;
}
1、安装Microsoft's C/C++ VS Code extension以添加对调试 C++ 文件的支持。
2、创建构建任务。打开命令选项板,找到 Tasks: Configure Tasks 然后 select 一个合适的 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、创建启动任务。打开调试面板。如果您是第一次这样做并且没有现有的启动配置,只需单击 创建 launch.json 文件 link:
如果您已有配置,请打开下拉菜单并select 添加配置。
它应该会打开现有的 launch.json 文件并向您显示要使用的启动配置类型的弹出窗口。 Select C++ 与 Launch
像这样更新配置:
{
// 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)。请注意,在 launch.json 配置中,我将 stopAtEntry
设置为 true
,让我有机会看到外部控制台 window然后向提示提供输入。如果你不需要它,你可以删除它。
如果一切顺利,它将通过启动外部控制台运行。
有关详细信息,请参阅 VS Code 的 Configuring C/C++ debugging 文档中的完整设置指南。
其实很简单。打开一个新的 window 的 VS 代码并打开终端。并复制粘贴编译和 运行 C++ 程序的确切命令。因此,您有 1 个 window 用于代码浏览,另一个用于程序执行。
Windows
的 VS 代码这是
其他参考文献https://code.visualstudio.com/docs/cpp/launch-json-reference
您首先需要安装 Microsoft's C/C++ VS Code extension,然后才能使用此启动配置。
然后像这样配置您的 launch.json
文件(取消注释您可能需要的任何可选参数):
{
"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",
}
]
}