使用VSCode编译并运行C++

发布于 2022-10-15  521 次阅读


AI 摘要

本文介绍如何使用VS Code进行C++的编译和运行。首先需要安装Mingw-w64,并在cmd中测试是否安装成功。然后,在.vscode文件夹中创建launch.json和tasks.json文件。其中,launch.json包含了C/C++的调试配置信息,指定了编译后程序的路径和运行环境等内容;tasks.json包含了编译、构建和调试等任务的配置信息,指定了编译命令和输出文件等参数。按照以上步骤配置好后,即可使用VS Code进行C++项目的编译和运行。
本文json来自 https://zhuanlan.zhihu.com/p/77645306

安装Mingw-w64,打开cmd,输入gcc -v,测试是否安装成功。

新建文件夹.vscode,新建launch.jsontasks.json

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "preLaunchTask": "compile",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        },
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "compile",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}