header-fileslanguage-server-protocolclangd

Force clangd to treat .h file as C header


Clangd LSP in Neovim keeps giving C++ diagnostics for an .h file. How do I force it to give C header diagnostics?


Solution

  • First, ensure vim is reading a .h file as C and not C++ syntax: Vim Airline interpreting .h files as cpp, not c

    let g:c_syntax_for_h = 1 
    

    (in kickstart init.lua, use vim.g.c_syntax_for_h = 1)

    Then in clangd's config.yaml, force .h files to use the flag -xc-header

    If:
      PathMatch: .*\.h
    CompileFlags:
      Add: -xc-header
    

    (separate If blocks with ---)