“g++”不被识别为内部或外部命令、可操作程序或批处理文件。

S.X*_*.Xu 6 c++ gcc visual-studio-code

我正在尝试使用 C++ 编译器设置 VScode 按照本教程 在此处输入链接描述

我遇到的问题是(通过使用代码运行器运行代码)

“g++”不被识别为内部或外部命令、可操作程序或批处理文件。 在此输入图像描述

如果我使用调试选项运行我会遇到这个问题

preLaunchTask“构建和运行文件”以退出代码 1 终止。在此处输入图像描述

这是我的“c_cpp_properties.json”

{
"configurations": [
    {
        "name": "Win32",
        "intelliSenseMode": "clang-x64",
        "defines": [
            "_DEBUG",
            "UNICODE",
            "__GNUC__=7",
            "__stdcall=attribute((stdcall))",
            "__cdecl=__attribute__((__cdecl__))",
            "__cplusplus=201703L"
        ],
        "includePath": [
            "${workspaceFolder}/include",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../include",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed",
            "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/include"
        ],
        "browse": {
            "path": [
                "${workspaceFolder}/include",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../include",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed",
                "C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/include"
            ],
            "limitSymbolsToIncludedHeaders": false,
            "databaseFilename": ""
        },
        "cStandard": "c11",
        "cppStandard": "c++17"
    }
],
"version": 4
Run Code Online (Sandbox Code Playgroud)

}

这是我的“launch.json”

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Run C/C++",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:/MinGW64/bin/gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "build & run file"
    },
    {
      "name": "Debug C/C++",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:/MinGW64/bin/gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "build & debug file"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的“tasks.json”

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build & debug file",
      "type": "shell",
      "command": "g++",
      "args": [
        "-g",
        "-o",
        "${fileBasenameNoExtension}",
        "${file}"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "build & run file",
      "type": "shell",
      "command": "g++",
      "args": [
        "-o",
        "${fileBasenameNoExtension}",
        "${file}"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我只是想学习C++,我在互联网上尝试了很多方法,但我仍然无法解决它,请帮忙。谢谢。

Sau*_*pal 5

您必须将 g++ 添加到您的 PATH 变量中。

首先,查一下你是否安装了g++并找到它所在的位置。

您还没有提到您正在使用什么操作系统。如果您使用的是 Windows,它可能位于C:\mingw或中C:\Program Files\mingw

如果你还没有安装g++,可以通过https://sourceforge.net/projects/mingw-w64/安装它

然后打开System Properties -> Advanced -> Environment Variables

然后在Environment Variables for <username>选择下PATH单击编辑。

单击新建,然后单击浏览并找到binmingw 安装中的目录并将其添加到路径中,然后就完成了。

如果打开 VSCode,请重新启动它,它会自动找到 g++。

  • 鉴于他们使用的是 MinGW(***Windows*** 的 Minimalist GNU),所以可以肯定他们使用的是 Windows :-) (4认同)