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

How to fix "g++: error: helloworld.cpp: No such file or directory" in visual Studio Code with WSL installed?

本文关键字:helloworld error cpp g++ such directory or file No WSL 安装      更新时间:2023-10-16

我在W10上安装了Visual Studio Code,以便在安装了WSL(Ubuntu)的情况下运行一些代码。

我遵循了以下文章中的步骤:

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

但当我试图在visual Studio代码中编译代码时,我一直收到以下错误消息:

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

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

tasks.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代码应该通过文件夹C:\Users\Marc\projects\helloworld.vscode中的WSL运行,该文件夹当前包含:

c_cpp_properties.json

helloworld.cpp

launch.json

tasks.json

如何解决这个问题?

谢谢

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

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

目录结构应该如下所示:

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

->->.vscode

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

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

我也遇到了同样的问题。我来这里寻求解决方案,但没有找到。我尝试了我能做的一切,但最后,我做了一个简单的改变,解决了问题。我把我的代码命名为";快速排序.cpp";错误之前。现在我把它改成了";QuickSort.cpp";,命名时没有在中间留出任何空格。现在它正在成功运行。这可能是您出错的原因,也可能不是。但要确保你没有犯这个错误。

只是不要用任何空白字符保存文件,即单个空格/tab。。。。

保存不带空格的文件(文件名不带空格)并使用c++扩展名

似乎编译器无法定位源文件,请更新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}给出了文件的完整路径。

保存时,文件名不应包含空格例如。不命名Hello world将其命名为Hello_world

Mhmm!在为文件命名时,您似乎没有遵循正确的命名方案。例如:Hello World.cpp会导致此错误。但是,如果您使用Hello_World.cpp,那么它完全可以正常工作。

在连接文件名的两个单词时,请尝试使用underscrore(_)

将文件保存在helloworld的文件夹中,如projects\helloworld\

相关文章: