Using vscode-clangd Fails to locate Arduino.h

Due to cross development, I use the Visual Studio Code to do C++ development for work. I love being able to use the PlatformIO IDE for my Arduino hobby. Unfortunately I came across an issue that I can work around, just not sure why I have to.

When I have the Clangd Language Server installed and enabled the Text editor will no longer find the Arduino.h file giving that nice red Squiggly under the name. Weird thing it it compiles and uploads just fine with no issues. I can modify the code, upload it and make changes. Only thing is you get all these “Problems” that are not problems… it just cant seem to find <Arduino.h> so Intellisense fails to work… It fails to locate any of the libraries used, yet compiles just fine.

If I go to the vscode-clangd and “Disable (Workspace)” then reload… everything works again as advertised. Obviously the workaround works… but why? I don’t do this with other extensions, only PatformIO. What is it about its clang integration that fails with the new clangd server?


My environment is:

C:\Users\tom\Documents\PlatformIO\Projects\180203-011250-feather32u4>pio platform list
atmelavr ~ Atmel AVR

Atmel AVR 8- and 32-bit MCUs deliver a unique combination of performance, power efficiency and design flexibility. Optimized to speed time to market-and easily adapt to new ones-they are based on the industrys most code-efficient architecture for C and assembly programming.

Home: PlatformIO Registry
Frameworks: arduino, simba
Packages: toolchain-atmelavr, framework-arduinoavr, framework-simba, tool-avrdude, tool-micronucleus
Version: 1.8.1

atmelavr ~ Atmel AVR

Atmel AVR 8- and 32-bit MCUs deliver a unique combination of performance, power efficiency and design flexibility. Optimized to speed time to market-and easily adapt to new ones-they are based on the industrys most code-efficient architecture for C and assembly programming.

Home: PlatformIO Registry
Frameworks: arduino, simba
Packages: toolchain-atmelavr, framework-arduinoavr, framework-simba, tool-avrdude, tool-micronucleus
Version: 8a06be6

C:>echo %PATH%
C:\Python27;C:\Python27\Scripts;C:\WinAVR-20100110\bin;C:\WinAVR-20100110\utils\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\PuTTY;C:\Development\Microchip\MPLAB C32 Suite\bin;C:\Development\HI-TECH Software\PICC\PRO\9.65\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Program Files\Git\cmd;C:\Users\tom\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Nmap;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;;C:\LLVM\bin;C:\Development\Microsoft VS Code Insiders\bin

C:>git --version
git version 2.16.1.windows.1

C:>clang --version
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\LLVM\bin

C:>python --version
Python 2.7.14

C:>

1 Like

More information.
This is the LIST of Problems when “clangd” is enabled. Again… no issues when its disabled

Simple BASE install… Minimal Extensions.

VS_Code_Build_Installed

Any help in understanding the discrepancy would be greatly appreciated.

We don’t use Clang and use Microsoft C/C++ extension. It seems that it conflicts.

Phew, that was a journey. I withdrew the post above, because it actually just broke clangd (hence no errors!)

There’s a partial solution by adding the following:

Create a file called config.yaml in %LocalAppData%\clangd\, with the following contents:

#%LocalAppData%\clangd\config.yaml
CompileFlags:
  Add: "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino"

This will mean files will have autocomplete:
image

But there’s a squiggly line under Arduino.h because of some weird C++ issues, unfortunately :frowning:

PlatformIO will always be using the Arduino core from its packages, never the globally installed one, so you should be rather pointing it to C:\Users\<user>\.platformio\packages\framework-arduino-avr\cores\arduino.

PlatformIO still not supports clangd natively (also used in the e.g. OSS version of VSCode) because of outstanding bugs.

2 Likes

I’ve been able to figure this out after whole day of researching LOL

I needed this as I use AstroNvim and that has clangd suppost, but setting up ccls manually is pain and even after you manage to do it, you get some stupid warning: multiple different client offset_encodings detected for buffer, this is not supported yet.

So, to do this, you need to

  • Add this to your platformio.ini:
    build_flags = -Ilib -Isrc
  • And then you need to run this command:
    pio run -t compiledb

This will generate a file called compile_commands.json which sets all the include paths for all the files.
You may have to regenerate the file when you add some new files (with the pio command above), as it looks as it’s file dependent and does not set the include paths recursively for all the files.

But IDK, I’ve been using PlatformIO with VSCode before so I wasn’t really messing around with compilers and compile_commands.json.

But hey, it looks like it’s working, so I hopefully helped.