Get clangd to find standard headers in nix-shell

I seem to have found a solution to the cmake-failing-to-find-standard-C+±headers problem.

Firstly, adding

set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

to my CMakeLists.txt removes the need to specify -DCMAKE_EXPORT_COMPILE_COMMANDS=1 on the cmake CLI.

Secondly, adding

if(CMAKE_EXPORT_COMPILE_COMMANDS)
  set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES 
      ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
endif()

seems to make the cmake-generated compile_commands.json give clangd the required information to find standard headers, in addition to the project specific information.

This solution seems to be working quite well for me.

14 Likes