如何在安装了 WSL 的 Visual Studio Code 中修复“g++: error: helloworld.cpp: No such file or directory”?

qwe*_*805 5 c++ json visual-studio-code

我在 W10 上安装了 Visual Studio Code 来运行一些安装了 WSL 的代码(Ubuntu)。

我按照以下文章中的步骤操作:

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

但是在尝试在 Visual Studio Code 中编译代码时,我不断收到以下错误消息:

"g++: error: helloworld.cpp: No such file or directory"
Run Code Online (Sandbox Code Playgroud)

3 .json 文件的配置如下:

c_cpp_properties.json

{
"configurations": [
    {
        "name": "Win32",
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "/usr/bin/g++",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x64",
        "browse": {
            "path": [
                "${workspaceFolder}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        }
    }
],
"version": 4
}
Run Code Online (Sandbox Code Playgroud)

启动文件

{
"version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/marc/projects/helloworld/helloworld.out",
            "args": ["-fThreading"],
            "stopAtEntry": true,
            "cwd": "/home/marc/projects/helloworld/",
            "environment": [],
            "externalConsole": true,
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "pipeTransport": {
                "pipeCwd": "",
                "pipeProgram": "c:\\windows\\sysnative\\bash.exe",
                "pipeArgs": ["-c"],
                "debuggerPath": "/usr/bin/gdb"
            },
            "sourceFileMap": {
                "/mnt/c": "${env:systemdrive}/",
                "/usr": "C:\\Users\\Marc\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr\\"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

任务文件

{
"version": "2.0.0",
"windows": {
    "options": {
        "shell": {
            "executable": "c:\\windows\\sysnative\\bash.exe",
            "args": ["-c"]
        }
    }
},
"tasks": [
    {
        "label": "build hello world on WSL",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "/home/marc/projects/helloworld/helloworld.out",
            "helloworld.cpp"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

}

我在 WSL Ubuntu 上的路径项目是:

/home/marc/projects/helloworld/
Run Code Online (Sandbox Code Playgroud)

该文件夹是空的,因为 Visual Studio Code 应该通过 WSL 运行在当前包含的文件夹 C:\Users\Marc\projects\helloworld.vscode 中:

c_cpp_properties.json

helloworld.cpp

启动文件

任务文件

如何解决这个问题?

谢谢

met*_*ter 3

如果有人遇到这个问题,我在阅读官方教程后使用 VS Code 设置了 gcc

我遇到了同样的问题,解决方案是将 cpp 和头文件移动到项目文件夹(向上 1 个文件夹)中,即“.vscode”文件夹之外。

dir 结构应该如下所示:

-> 项目目录(项目根文件夹)

-> -> .vscode

-> -> -> json 文件在这里(在 .vscode 内)

-> -> helloworld.cpp (项目文件位于项目目录中)