VScode配置c/c++环境(无数试错版本)

VScode配置c/c++环境(无数试错版本)

在配置python时比较简单,c/c++有点复杂。

安装VScode

这个就不多说了,官网下载即可,或者应用市场下载。

下载mingw64并配置环境变量

建议采用离线下载,可参考以下文章,写的很详细。
下载安装MinGW-w64详细步骤(c/c++的编译器gcc的windows版,win10真实可用)

VScode和mingw64配置

首先新建一个文件夹作为c语言的项目文件,注意文件中不要含有汉字。
之后创建一个.vscode文件。包含以下三个文件。
在这里插入图片描述

  • c_cpp_properties.json
    在这里插入图片描述在这里插入图片描述
    将这两部分改成自己存放mingw64的路径
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "D:/LenovoSoftstore/Install/mingw64/include/**",   //修改成自己的路径
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "D:/LenovoSoftstore/Install/mingw64/include/**",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            },
            "cppStandard": "c++17",
            "cStandard": "c17"
        }
    ],
    "version": 4
}
  • launch.json
    将miDebuggerPath改成自己的路径
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\System32\\cmd.exe",   //解决闪退问题
            "args": ["/c","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\LenovoSoftstore\\Install\\mingw64\\bin\\gdb.exe",        /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++"
        }
    ]
}

解决终端闪退问题:在这里插入图片描述

  • tasks.json
    直接复制即可
{
    "version": "2.0.0",
    "command": "g++",
    "args": [
           "-g",
           "${file}",
           "-o",
           "${fileBasenameNoExtension}.exe",
           "-fexec-charset=GBK" //解决中文乱码问题
       ],
    "problemMatcher": {
       "owner": "cpp",
       "fileLocation": [
           "relative",
           "${workspaceRoot}"
       ],
       "pattern": {
           "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
           "file": 1,
           "line": 2,
           "column": 3,
           "severity": 4,
           "message": 5
       }
   }
}

解决中文乱码问题:在这里插入图片描述

测试

  • c文件测试

在这里插入图片描述

  • cpp文件测试
    在这里插入图片描述
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值