clangd on windows in neovim

Hey all, hope you're having a good day.

I'm competely new to c programming and I want to follow along to handmade hero to learn.

I don't however, want to go through it without having my basic LSP working in Neovim.

Has anyone else done this?

I cannot get it to recognize anything, it doesn't load header files.

I'm super lost as to what to do to get set up, and there isn't any lsp related setup resources that i could find.

it's only sublime text and atom, which if i remember correctly atom is defunct?

I'm on Windows 10, and use neovim with Clangd. or.. i want to.. any advice?

I expect clangd or any similar tools will not work well on Handmade Hero source code because its code base builds as unity build. Meaning .cpp files won't find correct declarations because includes are maintained in place where .cpp file itself is included. The .cpp files are not standalone translation units in such build.

Indeed. See this open issue: https://github.com/clangd/clangd/issues/45

I haven't found an existing solution for go-to-definition that works reliably for code bases structured this way.


Replying to mmozeiko (#29283)

Posting on this old thread in case anyone stumbles upon this in the future.. as it totally looks like the clangd team isn't willing to work on proper support for this, it seems that workarounds is all we have..

After fighting with this myself for a long time, the best workaround I have found yet is the one described in https://www.frogtoss.com/labs/clangd-with-unity-builds.html.

Essentially, assuming unity.cpp is your main (only) translation unit that includes all other .cpp files PLUS any necessary headers etc, what you do is:

  • Add #pragma once to the top of this file
  • Add #include "unity.cpp" to the top of every other .cpp file
  • Profit!

What this does is, it leaves your unity build unaffected, since the circular include in each .cpp component will be ignored (due to the #pragma). However, assuming your clangd is configured in the typical manner to compile each of your .cpp files individually, each of these compilations should now succeed since each .cpp file includes everything it needs at the very top.

This is probably still not perfect but it's miles better than the default clangd behaviour. I'm still testing out some kinks, my unity build is actually broken down per engine module, so there are definitely more complications there..

If anybody figures out a better system, I'd be super interested in hearing about it!