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 文件并运行。成功。

相关推荐
clint4561 天前
C++进阶(1)——前景提要
c++
夜悊2 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0012 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
LDR0062 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术2 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园2 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob2 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享2 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.2 天前
C语言--day30
c语言·开发语言