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