"g++"未被识别为内部或外部命令、可执行程序或批处理文件。

6
我正在尝试使用C++编译器在VScode中进行设置,按照这个教程进行操作:链接
但是我遇到了问题(通过使用Code Runner运行代码):
“g++”不被识别为内部或外部命令、可操作的程序或批处理文件。链接 如果我使用调试选项运行,我会遇到这个问题: preLaunchTask“build & run file”以退出代码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

}

这是我的 "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"
    }
  ]
}

这是我的“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
      }
    }
  ]
}

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


3
可能是重复问题:['g++' is not recognized as an internal or external command, operable program or batch file] (https://dev59.com/UVkT5IYBdhLWcg3wZusA)。 - Ken White
关于 "command": "g++",你可能需要包含完整的位置(包括目录),除非你确定包含它的目录实际上已经在你的标准路径中。 - paxdiablo
Ken White,那篇帖子对我没有帮助。 - S.Xu
这个回答解决了你的问题吗?"g++" is not recognized as an internal or external command, MinGW - Donald Duck
4个回答

5

您需要将g++添加到您的PATH变量中。

首先,找出是否已安装g++并找到其位置。

您没有提及您使用的操作系统。如果您使用的是Windows,则它可能位于C:\mingwC:\Program Files\mingw

如果您尚未安装g++,可以通过https://sourceforge.net/projects/mingw-w64/进行安装。

然后打开系统属性 -> 高级 -> 环境变量

然后在<username>的环境变量下选择PATH并单击编辑。

单击新建,然后单击浏览,找到您的mingw安装中的bin目录,将其添加到路径中即可完成。

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


3
考虑到他们正在使用MinGW(面向Windows的极简GNU编译器),可以合理地猜测他们正在使用Windows :-) - paxdiablo

2
[Sloved] 我遇到了同样的问题,我解决了它:
只需要添加一个新路径:"C:\MinGW\bin\g++.exe"
现在重新打开您的VS代码,问题就解决了: enter image description here

0
首先,打开这个页面并在您的系统上安装和配置Mingw:

http://www.mingw.org/wiki/getting_started

在下载和安装后,您必须将链接中的Mingw->bin添加到您的路径中:

https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/

然后为了确保,可以在CMD中输入g++ --versiongcc --version

然后您可以使用此链接来达到您的目的。

如需更多帮助,请使用

https://code.visualstudio.com/docs/cpp/config-mingw

这个链接对你来说可能非常有用


最好避免在答案中传递外部链接。如果您要引用外部资源,请同时概述您所引用的外部参考资料中所描述的内容。 - Patrick

0

你需要添加mingw。

首先,找出是否已安装g++并找到其位置。

  1. 首先将mingw文件复制到C驱动器中。 mingw下载链接:https://drive.google.com/file/d/112lN_esKLgOJ-bXbw7iGgbTOU5wKfW-_/view?usp=sharing 1

  2. 如果您使用的是Windows,则可能位于C:\mingw\bin。

2

然后打开系统属性 -> 高级 -> 环境变量 -> 编辑

3

  1. 然后点击新建 -> 粘贴链接 C:\mingw\bin 然后点击确定。

4

如果您已经打开了VSCode,那么请重新启动它,它会自动找到g++。

2
你是否发布了指向你个人副本的mingw安装程序链接?为什么不发布指向官方 mingw 页面的链接呢? - JarMan
1
不是一个好主意。我不会相信没有来自官方来源的可执行文件。 - JarMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接