Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Last active October 21, 2025 23:29
Show Gist options
  • Select an option

  • Save wilyJ80/c2c515f86f62e2aba1df586e4ae69b91 to your computer and use it in GitHub Desktop.

Select an option

Save wilyJ80/c2c515f86f62e2aba1df586e4ae69b91 to your computer and use it in GitHub Desktop.
CLANGD + ARDUINO + PLATFORMIO
  1. Select board and generate project
  2. Add dependencies to platformio.ini
  3. Generate compile_commands.json with pio run -t compiledb
  4. Check if there are any errors in a sketch with #include <Arduino.h> related to compiler flags
  5. Ignore them in the .clangd configuration file:
CompileFlags:
  # Remove the flags that clangd doesn't understand
  Remove:
    - "-mlongcalls"
    - "-fstrict-volatile-bitfields"
    - "-fno-tree-switch-conversion"
  1. Run pio project metadata to display the necessary Arduino includes for clangd
"toolchain": [
"/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0",
"/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/xtensa-esp32-elf",
"/home/vic/.platformio/packages/toolchain-xtensa-esp32/lib/gcc/xtensa-esp32-elf/8.4.0/include-fixed",
"/home/vic/.platformio/packages/toolchain-xtensa-esp32/lib/gcc/xtensa-esp32-elf/8.4.0/include",
"/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include"
]
  1. Add these includes to .clangd
CompileFlags:
  # Add the missing system include paths manually
  Add:
    - "-isystem"
    - "/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0"
    - "-isystem"
    - "/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/xtensa-esp32-elf"
    - "-isystem"
    - "/home/vic/.platformio/packages/toolchain-xtensa-esp32/lib/gcc/xtensa-esp32-elf/8.4.0/include-fixed"
    - "-isystem"
    - "/home/vic/.platformio/packages/toolchain-xtensa-esp32/lib/gcc/xtensa-esp32-elf/8.4.0/include"
    - "-isystem"
    - "/home/vic/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include"
    
  # Remove the flags that clangd doesn't understand
  Remove:
    - "-mlongcalls"
    - "-fstrict-volatile-bitfields"
    - "-fno-tree-switch-conversion"
  1. Restart clangd and verify
@wilyJ80
Copy link
Author

wilyJ80 commented Oct 19, 2025

For compatibility with Arduino IDE, main file must be .ino and placed in the root project path with same name as it

@wilyJ80
Copy link
Author

wilyJ80 commented Oct 21, 2025

Make sure the clangd includes match the board

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment