Visual Studio Code无法找到g++.exe(Windows 10)

3

我正在按照这个教程在我的Windows电脑上设置vs code。

我通过从其他文件夹调用g++来检查路径是否正确。

但是我在vs code上一直收到这个错误:

Cannot find "C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw64\bin\g++.exe".

尝试使用命令提示符从存放 helloworld.cpp 文件的文件夹运行 g++ helloworld.cpp 命令,没有错误也没有输出。

有人能告诉我错过了什么吗?

编辑:

代码已经编译完成。 它生成了 a.exe 并且可以正常运行。 问题是我需要将其集成到 VS Code 中。 我猜这类似于 这个 问题,但我不确定。

c_cpp_properties.json

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw64/bin/g++.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

已纠正tasks.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558 
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "helloworld",
            "helloworld.cpp"
        ],

        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

tasks.json

    {
// See https://go.microsoft.com/fwlink/?LinkId=733558 
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "helloworld",
            "helloworld.cpp"
        ],
        "options": {
            "cwd": "C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw64/bin/"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

settings.json

{
"[cpp]": {},
"terminal.integrated.shell.windows": "C:\\cygwin64\\bin\\bash.exe"

}

launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/helloworld.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw64/bin/g++.exe",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]

}

3个回答

0
tasks.json 文件中,您有:
"tasks": [
    {
        ...
        "command": "g++",
        ...
        "options": {
            "cwd": "C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw64/bin/"
        },
        ...

但是你应该使用当前工作目录

"cwd": "${workspaceFolder}"

我想我找到了我的错误。 我已经更新了上面的tasks.json。 - Codelearner777

0

你确定路径正确吗?通常如果它说找不到文件,那是因为文件不存在。

当我安装MinGW-W64时,它会安装到C:(...)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32。所以我认为你的路径是错误的,正如消息所说的那样(除非你手动更改了它):它不应该在mingw64\bin中,而应该在mingw32\bin中。


我在Windows上检查路径是否正确。是的,我需要更正JSON文件中的路径。更正后,错误码现在是1。之前是2。错误信息: g++.exe: error: f:XXXYYYYcpphelloworld.cpp: No such file or directory 看来它无法识别"/"字符。 - Codelearner777

0

它是否有效? 我认为你的 tasks.json 应该是:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558 
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "g++.exe build active file",
                "command": "g++",
                "args": [
                    "-g",
                    "helloworld.cpp",
                    "-o",
                    "helloworld"
                ],
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }

关键部分是添加正确的cwd选项。在这个配置中,“helloworld”应该位于您的workspaceFolder下面。如果不是这种情况,请相应地更新helloworld.cpp的路径。

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