最新版vscode中设置C++代码在CMD命令框中运行
使用快捷键Ctrl + Shift + B后,选择生成g++ 调试文件
将会有这两个文件,点开launch.json文件, 更改以下注释了的内容
{
"configurations": [
{
"name": "C/C++: g++.exe 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true, //是否用win终端展示
"MIMode": "gdb",
"miDebuggerPath": "your_mingw_path", //填自己的文件路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
],
"version": "2.0.0"
}
保存然后退出。
接着打开task.json文件,复制以下代码并做修改
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "your_mingw_g++_path", //填写自己g++.exe程序所在路径
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\a.exe",
"-fexec-charset=GBK"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
最后注意在你要执行的代码中添加system(“pause”);,这代码在CF提交是被允许的,并且不会报答案错误。
#include <iostream>
using namespace std;
int main()
{
cout << "hello world!";
system("pause"); // 使终端执行完,不会自动关闭
return 0;
}