VSCode tasks.json 2.0中g++提示“No such file or directory”问题求助
Hey there! Let’s work through this issue—since you can compile just fine from the command line, the problem isn’t with your system’s g++ setup or your include paths in c_cpp_properties.json. It’s almost certainly how your tasks.json is referencing your files or calling g++. Here’s what to check:
1. Double-check file paths in your tasks.json
VSCode’s task system uses paths relative to your workspace root (unless you use absolute paths). If your Subclass.cpp is at /home/superben/ESP32VSC/CodeExamples/Inheritance/, make sure:
- Your VSCode workspace is opened directly to that folder (not a parent directory)
- Your
tasks.jsoncorrectly references the file. For compiling the currently active file, use the built-in variable${file}which automatically points to the open file. A working single-file build task looks like this:
If compiling multiple files, list each one with the correct path—use{ "version": "2.0.0", "tasks": [ { "label": "Build with g++", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] }${workspaceFolder}to reference your workspace root, e.g.,"${workspaceFolder}/Subclass.cpp".
2. Use g++’s absolute path in tasks.json
Sometimes VSCode’s shell environment doesn’t pick up the same PATH as your terminal. Run which g++ in your command line to get the full path (e.g., /usr/bin/g++), then replace the command field in tasks.json with this absolute path:
"command": "/usr/bin/g++"
3. Remember: c_cpp_properties.json doesn’t control compilation
Just a quick clarification—c_cpp_properties.json is only for IntelliSense (code highlighting, auto-completion, syntax checks). It doesn’t affect how the build task finds files or runs g++, so even if you added the path there, tasks.json still needs to handle file references correctly.
Quick test to debug paths
If you’re still stuck, add a temporary task to print the current working directory:
{ "label": "Check current dir", "type": "shell", "command": "pwd", "problemMatcher": [] }
Run this task and verify the output matches the directory where Subclass.cpp lives. If not, you might have opened the wrong workspace folder in VSCode.
内容的提问来源于stack exchange,提问作者user2565677

