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

新手上路,请多包涵

我在 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"

有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
}

启动.json

 {
"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\\"
            }
        }
    ]
}

任务.json

 {
"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
        }
    }
]

}

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

 /home/marc/projects/helloworld/

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

c_cpp_properties.json

你好世界.cpp

启动.json

任务.json

如何解决这个问题?

谢谢

原文由 qwerty1805 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 718
2 个回答

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

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

dir 结构应如下所示:

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

-> -> .vscode

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

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

原文由 metablaster 发布,翻译遵循 CC BY-SA 4.0 许可协议

似乎编译器无法找到源文件,更新 tasks.json 以使用完整路径编译您的代码,

 "tasks": [
    {
        "label": "build hello world on WSL",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "/home/marc/projects/helloworld/helloworld.out",
            "${file}"//Just change this
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

${file} 给出文件的完整路径。

原文由 parth_07 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题