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(调试配置)‌:定义调试行为

配置 C/C++ 调试

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

设置 "externalConsole": true 时会直接运行编译后的 exe 文件,

如果需要看 log ,在 cpp 文件中加 system("pause"),防止可执行文件退出。

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

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

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

或是直接按 F5

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐