VS Code配置C/C++

VS Code 配置C/C++

 准备工作
Mingw-w64下载,在vscode中,点击帮助文档,进入如下界面。

在这里插入图片描述

在左侧选择C++Mingw-w64 onWindows,进入如下界面。可以参考文档进行配置。
在这里插入图片描述

点击文档中的mingw-w64链接进入如下界面。
在这里插入图片描述

点击黄色区域链接进入到里面,再点击下图中的文字,进入到下载界面,下载即可。
在这里插入图片描述

将其自定义安装(我的安装在了mingw文件夹下),记住配置其路径,D:\mingw\mingw64\bin。

 扩展vscode插件,如下。
在这里插入图片描述

 .json文件配置,C/C++需要三个配置文件。
• c_cpp_properties.json
• tasks.json
• launch.json
可以参看文档的配置,也可以根据自己的情况设置。

对于配置文档的一些介绍:
对于launch.json文件,“configurations”: [],中括号里是书写配置信息的,每一个配置信息,由一个{}包括, {}由逗号分隔,每个配置都会在如下图的位置显示出来,便于调用:
在这里插入图片描述

对于每一个{}包含的配置的具体项介绍如下:



        {
            "name": "C++ Run(minGW64)",
            "type": "cppdbg",
            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw/mingw64/bin/gdb.exe",
            "args": [],
            "stopAtEntry": false,
            "environment": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": false,
            "preLaunchTask": "C++ Run",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        },

Name的值可以人为设定,会在上述位置显示,便于区分,而"preLaunchTask": "C++ Run"这个的值,则对于tasks.json中的一个任务,其值必须和label的值对应,如下:


{
            "label": "C++ Run",
            "type": "shell",
            "command": "g++",
            "args": 
            [
                "-o",
                "${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": 
            {
                "kind": "build",
                "isDefault": true
            }

可以设置多对上述的对应关系,用于不同语言的编译调试。

下面是我的这几个文件的具体配置:
 c_cpp_properties.json


{
    "configurations": [
        {
            "name":"C++/Win32",
            "includePath": 
            [
            
                "${workspaceRoot}",
                "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                "D:/mingw/mingw64/include"
            ],
            "defines": 
            [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "clang-x64",
            "browse":
            {
                "path": 
                [
                    "${workspaceRoot}",
                    "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
                    "D:/mingw/mingw64/include"
                ]
            },
            "compilerPath": "D:/mingw/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

 launch.json


{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "CodeLens (Launch) - DigitalImage",
            "request": "launch",
            "mainClass": "app.DigitalImage",
            "projectName": "main"
        },
        {
            "name": "C++ Run(minGW64)",
            "type": "cppdbg",
            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw/mingw64/bin/gdb.exe",
            "args": [],
            "stopAtEntry": false,
            "environment": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": false,
            "preLaunchTask": "C++ Run",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        },
        {
            "name": "C++ Debug(minGW64)",
            "type": "cppdbg",
            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw/mingw64/bin/gdb.exe",
            "args": [],
            "stopAtEntry": false,
            "environment": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": true,
            "preLaunchTask": "C++ Debug",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        },

        {
            "name": "C Run(minGW64)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw/mingw64/bin/gdb.exe",
            "args": [],
            "stopAtEntry": false,
            "environment": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": true,
            "preLaunchTask": "C Run",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        }
        ,
        {
            "name": "C Debug(minGW64)",
            "type": "cppdbg",
            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw/mingw64/bin/gdb.exe",
            "args": [],
            "stopAtEntry": false,
            "environment": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": true,
            "preLaunchTask": "C Debug",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}

 tasks.json


{
    "version": "2.0.0",
    "tasks": [
        //C++ Run Tasks
        {
            "label": "C++ Run",
            "type": "shell",
            "command": "g++",
            "args": 
            [
                "-o",
                "${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": 
            {
                "kind": "build",
                "isDefault": true
            }
        },
        //C++ Debug Tasks
        {
            "label": "C++ Debug",
            "type": "shell",
            "command": "g++",
             "args": 
            [
                "-g",
                "-o", 
                "${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": 
            {
                "kind": "build",
                "isDefault": true
            }
        },
        //C Run Tasks
        {
            "label": "C Run",
            "type": "shell",
            "command": "gcc",
            "args": 
            [
                "-o",
                "${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": 
            {
                "kind": "build",
                "isDefault": true
            }
        }
        ,
        //C Debug Tasks
        {
            "label": "C Debug",
            "type": "shell",
            "command": "gcc",
             "args": 
            [
                "-g",
                "-o", 
                "${fileBasenameNoExtension}",
                "${file}"
            ],
            "group": 
            {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

参考链接:
https://www.cnblogs.com/TAMING/p/8560253.html
https://www.cnblogs.com/zhuzhenwei918/p/9057289.html
https://www.cnblogs.com/wanghao-boke/p/12076302.html
https://www.cnblogs.com/jpfss/p/10333911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秘境之眼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值