如何在安装了 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?
我在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\
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 构建失败错误:无法使用 qibuild 构建C++ SDK Helloworld
- 为什么我会"error: expected expression"?
- 如何在安装了 WSL 的 Visual Studio Code 中修复"g++: error: helloworld.cpp: No such file or directory"?