VS Code 配置C/C++环境 出现问题 could not find the task 'g++' / 'gcc'

前言

由于新电脑未装VSCode C/C++配置环境,刚好手头有些东西想在上面验证。
于是开启安装之旅,耗时大概4h…最后还是拷了旧电脑的配置…修改过后才解决的问题。
如果你是被标题“骗”进来的,请直接跳转到tasks.json部分。

推荐先序阅读

Visual Studio Code 如何编写运行 C、C++ 程序? — 谭九鼎

编译器

  • MinGWMinGW-w64 是两个不同的东西(MinGW 相对较老,还是用 MinGW-w64 ,还有不要下载错了,x86_64-posix-seh…文末分享安装包)
  • 环境变量未生效问题:这位兄台,要不你重启试试?

插件

  • C++ Intellisense
  • C/C++
    VS Code 配置C/C++环境 出现问题 could not find the task 'g++' / 'gcc'_第1张图片

C配置

launch.json

{  
    "version": "0.2.0",  
    "configurations": 
    [  

        {
            "name": "C", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "D:\\Mingw-w64\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "gcc", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": 
            [  
                {   
					"description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }
    ]
}

一般需要修改的应该只有miDebuggerPath而已
当然如果你想作为C/C++的环境,用g++编译.c文件也是可以的preLaunchTask修改为g++即可。

tasks.json

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++",
            "command": "D:/Mingw-w64/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:/Mingw-w64/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "shell",
            "label": "gcc",
            "command": "D:\\Mingw-w64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\Mingw-w64\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

注意:找不到任务的错误提示就是这里出现的,何解?
tasks.jsontaskslabel 项必须和 launch.jsonpreLaunchTask 相对应!!
此外,你可能需要修改的还有:commandoptions-cwd(都是路径,不需要解释了吧)

c_cpp_properties.json

这个文件的配置,建议通过Ctrl+Shift+P打开命令行,选择C/C++:编译配置(UI)

VS Code 配置C/C++环境 出现问题 could not find the task 'g++' / 'gcc'_第2张图片
打开后可以发现:配置名称编译器路径已经有了。
VS Code 配置C/C++环境 出现问题 could not find the task 'g++' / 'gcc'_第3张图片
如果没有的话,请先确保你的环境变量已经生效。
当然你也可以新建一个c_cpp_properties.json

VS Code 配置C/C++环境 出现问题 could not find the task 'g++' / 'gcc'_第4张图片

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/Mingw-w64/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

需要修改的内容compilerPath

C++配置

launch.json

{  
    "version": "0.2.0",  
    "configurations": 
    [  

        {
            "name": "C++", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "D:\\Mingw-w64\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": 
            [  
                {   
					"description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }
    ]  
}

需修改miDebuggerPath

tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

settings.json

{
    "files.associations": {
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "exception": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "type_traits": "cpp",
        "typeinfo": "cpp",
        "iomanip": "cpp"
    }
}

编译器下载

百度云盘链接
提取码: a8r8

你可能感兴趣的:(C/C++,VSCode,c++,C,环境配置)