I see that this has already been asked a few times, but this issue I’m having seems unique enough to warrant a new post.
I’ve just recently decided to try using PlatformIO for my ESP32 Arduino project. To get things started, all I did was install the VSCode extension and create a new project. However, the initial project that was generated gave me an include error: unable to open Arduino.h. After a bit of googling, I found a suggestion that said to erase the .vscode directory and then restart VSCode. Did that and then noticed the include error – this time, Intellisense is giving the error cannot open source file "algorithm" (dependency of "Arduino.h") After seeing this, I checked the include path of the compiler (echo | xtensa-esp32-elf-gcc -E -Wp,-v -) and then checked the include paths listed there – I found the algorithm file at /home/me/.espressif/tools/xtensa-esp32-elf/esp-2019r2-8.2.0/extensa-esp32-elf/xtensa-esp32-elf/include/c++/algorithm. The file is definitely there, but Intellisense doesn’t seem to recognize it!
I copied the file to my project’s include/ directory to see if that would solve the issue, but lo and behold, Intellisense now cannot find source file "utility"! I’d much prefer not to have to copy stdlib headers every time Intellisense starts complaining, so does anybody have an idea on how to resolve this?
Some info about my setup:
VSCode extensions: C/C++ (Microsoft), PlatformIO IDE, some Theme extensions, Vim emulator (vscodevim)
pio --version: PlatformIO, version 4.2.1a1
$ pio settings get
Name Current value [Default] Description
------------------------- ------------------------------------------ --------------------------------------------------------
auto_update_libraries No Automatically update libraries (Yes/No)
auto_update_platforms No Automatically update platforms (Yes/No)
check_libraries_interval 7 Check for the library updates interval (days)
check_platformio_interval 3 Check for the new PlatformIO interval (days)
check_platforms_interval 7 Check for the platform updates interval (days)
enable_cache Yes Enable caching for API requests and Library Manager
enable_telemetry Yes Telemetry service <http://bit.ly/pio-telemetry> (Yes/No)
force_verbose No Force verbose output when processing environments
projects_dir /home/colton/Documents/PlatformIO/Projects Default location for PlatformIO projects (PIO Home)
strict_ssl No Strict SSL for PlatformIO Services
There is no point in modifying c_cpp_properties.json as it is automatically generated by PlatformIO and will be overwritten without warning.
The algorithm header file should be in the directory /home/me/.platformio/packages/toolchain-xtensa32/xtensa-esp32-elf/include/c++/5.2.0 and should be found without adding it to c_cpp_properties.json as it is part of the compiler installation.
Try the following steps:
Make sure the project can be successfully built despite the warnings in the editor
Make sure your project path or PlatformIO installation path doesn’t contain any spaces or special characters (as Visual Studo Code’s IntelliSense is unable to deal with it)
Close all editor views in Visual Studio Code and quit VSC
Delete .vscode within your project folder again (as you have modified it)
Open Visual Studio Code again
Within Visual Studio, open main.cpp from the Explorer view
.vscode should now be recreated and after a few seconds the warning in the editor should go away
If you still have problems, post your platformio.ini file and information about the platform you are using (macOS?).
I didn’t make any edits to any files (except main.cpp, to consolidate – other than that, this project is entirely default). I simply copied the Algorithm file to the include directory (I didn’t add it to c_cpp_properties.json include path since I knew it was auto-generated, as I had deleted the .vscode directory before and the file was re-created).
I’ve followed your steps. The project compiles, but Intellisense is still complaining about not being able to open the algorithm file (since I’ve removed it from include/).
When comparing your log with a similar log on my machine (for basically the same ESP32 project), its very similar. But one thing pops out. Your log contains these lines (line 327 – 332):
Here’s the c_cpp_properties.json file. The intellisense mode seems to be using 64-bit clang, but the compiler triple from compilerPath does specify the xtensa esp32 compiler
Those directories are part of the gcc include path. I was having an issue where Intellisense wasn’t including header files that weren’t in an include path I was having gcc use (I set CPATH to include /home/colton/include). Because Intellisense was ignoring those headers, I had the idea of adding to Intellisense’s default include path. The instructions are here.
After thinking a little bit about #6 I remembered I had done this, deleted the changes, and voila, PlatformIO works! I have no idea why this was causing issues, but thank you so much for helping me figure this out!
The problem might be that it’s not adding, but overriding? A quick test on my end seems setting "C_Cpp.default.includePath" suggested absolutely nothing to the intellisense processing - didn’t add or change the folders processed as far as I could see, but that seems at odds with the first thing it says on that page:
You can override the default values for properties set in c_cpp_properties.json .
It looks like you need to add "${default}" to the parameter in c_cpp_properties.json if you want it to add the values configured via those flags… not sure how you would do that with PIO though, maybe by missusing the -Idirbuild_flags?
Do you know what specific settings you changed? Maybe it’s only one particular one that mucks things up, and could be a bug that needs reporting back to the VSCode C/C++ extension team.
A quick test of an ESP32 project shows I have the same behaviour as @manuelbl - toolchain-extensa32 lines after tool-unity…
@pfeerick I changed the settings at C_Cpp.default.systemIncludePath so that it included the extra directories that manuelbl had pointed out. I don’t believe I added "${default}", as I didn’t think there were default values for the system include path that I wasn’t already including. I can reproduce if you believe that it’s worth looking into for the c/c++ extension team.
If this setting has a value, then the system include path the extension gets from the compiler specified in the compilerPath setting will not be added to the path array that the extension uses for IntelliSense.
Which suggests this is the reason why the .platformio/packages/toolchain-xtensa32 paths were dropped… as it was no longer adding the include paths the compiler offered up… interesting… and quite nasty if you don’t realise it’s replacing part of the include path construction mechanic, not just allowing you to add it!