Search code examples
cvisual-studio-codecmakeproject

How to tell VS Code where to find header and source files in a CMake project?


I have a complex directory structure for a C project in which CMAKE governs which files are used for a certain project.

I tried using CMake extensions for VS Code, but it did not work very well.

Is there a way to tell VS Code which files exactly are used to be able to navigate through the code?


Solution

  • Open the Command Palette (F1 or Ctrl+Shift+P), look for "C/C++: Edit configurations (UI)", and add the desired folders under "Include path". This will allow Visual Studio Code's IntelliSense to know where your header files are located.

    For an optimized experience, don't forget to also set up the path to the compiler, the IntelliSense mode appropriate for that compiler, and the C/C++ standards you are using.

    PS: For more on how to use VS Code for C and C++ programming, you may be interested in these lecture notes I wrote for a graduate course I'm teaching.