vscode 怎么运行 c++ 文件

windows 配置 c++ 环境-CSDN博客

VSCode安装C/C++扩展‌:在VSCode扩展商店搜索并安装微软官方"C/C++"插件

在项目根目录创建 .vscode 文件夹,并添加以下文件:

‌**tasks.json(编译配置)**‌

复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C/C++: g++.exe build active file",
            "type": "cppbuild",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

其中args指定编译选项,如-g生成调试信息。‌‌

‌**launch.json(调试配置)**‌:定义调试行为

复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "cwd": "${fileDirname}",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "miDebuggerPath": "gdb",
            "externalConsole": true,
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

好了,现在新建个测试文件 hello.cpp

复制代码
#include <iostream>
using namespace std;
int main() {
    cout << "hello, world" << endl;
    system("pause");
    return 0;
}

打开运行和调试视图,点击运行,会编译出 exe 文件并运行。成功。

相关推荐
肆忆_21 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc