c++ - 'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。

标签 c++ gcc visual-studio-code

我正在尝试使用 C++ Compiler 设置 VScode
按照本教程
enter link description here

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

'g++' is not recognized as an internal or external command, operable program or batch file. enter image description here



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

The preLaunchTask 'build & run file' terminated with exit code 1.enter image description here



这是我的“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++,在网上试了很多天,还是解决不了,请大家帮忙。
谢谢你。

最佳答案

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

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

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

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

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

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

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

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

关于c++ - 'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975157/

相关文章:

c++ - 是否可以从 gcc 的源代码中确定或设置编译器选项?

terminal - 如何在 Visual Studio Code 中从集成终端引用当前文件

c++ - 删除 C++ 中的每个指针作为指向数组的指针是否安全?

c++ - 局部变量的安全

c++ - 模板化 ostream 重载歧义错误 : basic_ostream<char> vs const char[]

c - 如何将同一个函数放入两个 gcc 静态库中,使其具有不同的实例?

c++ - 尝试构建 muParser:错误:'std::basic_ostream 的显式实例化但没有可用的定义

c++ - 为什么不能显式默认带有 volatile 参数的复制构造函数?

javascript - VS 代码片段到当前行下方的 console.log 选择

r - 如何在不需要关闭之前的情节的情况下刷新情节?