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

标签 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"

有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

helloworld.cpp

启动.json

任务.json

如何解决这个问题?

谢谢

最佳答案

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

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

目录结构应如下所示:

-> project dir (project root folder)

-> -> .vscode

-> -> -> json files are here (inside the .vscode)

-> -> helloworld.cpp (project files are here inside project dir)

关于c++ - 如何在安装了 WSL 的 Visual Studio Code 中修复 "g++: error: helloworld.cpp: No such file or directory"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290300/

相关文章:

c++ - MSVS2010中 "%"宏和 "$"宏的区别

mysql - 发生错误: Syntax Error: Unexpected token y

json - Go中接口(interface)的自定义JSON序列化和反序列化

json - Cors JSON 输入意外结束

visual-studio-code - 在 VS 代码文件资源管理器中抑制二进制文件

c++ - 我在哪里可以获得 afx...h 文件?

c++ - 数组指针

c++ - 从使用不完整类型实例化的模板继承

visual-studio-code - Visual Studio语言扩展,如何调用自己的函数?

python - 使用 conda 环境在 VS code 中部署 Azure Functions