Visual Studio Code: C++ include path

37,890

Solution 1

Okay, this was foolish, but in the event someone uses Visual Studio Code and does not have a trivial project. These instructions are assuming you're using clang compiler:

  1. Open your project directory
  2. Open .vscode/settings.json
  3. Configure the line below inside of the JSON object:

    // Compiler options for C++ (e.g. ['-std=c++11'])
    "clang.cxxflags": [
        "-I/path/to/my/include/directory" // header files
    ],
    

Solution 2

If you are using CMake, VSCode has CMake plugins to help you build the project. So you do not need to modify the settings.json. Just use:

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") 

Or if there are no other modules used the header files in that folder you could use something like:

target_include_directories(MyHelper, "${CMAKE_CURRENT_SOURCE_DIR}/include") 

If you only need the project be built successfully. That is the whole story.

In the case of that, you got some little green zigzag lines under the #include statements hurting your eyes. You need to generate c_cpp_properties.json. It has nothing to do with helping the compiler to build the code but for helping VSCode intellisense to realize the existence of libraries and header files. And again, you are able to leverage the CMake by adding CMake options in the CMakeLists.txt:

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

The CMake will generate a file compile_commands.json under your build directory. The VSCode is able to parse the Json file and find the include path based on the content in that file. So what you need to do is just letting VSCode know where is the Json file. You can do that by adding follwing line in the c_cpp_properties.json:

 "configurations": [
        {
            "name": "Mac",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            ...
        }],

Rebuild the project will let the VSCode intellisense find all necessary paths.

[Environment]
Ubuntu: 16.04.3
VSCode: 1.23.1
VSCode plugins: C/C++ 0.17.0, CMAKE 0.0.17, CMakeTools 0.11.1

Share:
37,890
Deftness
Author by

Deftness

Updated on May 18, 2020

Comments

  • Deftness
    Deftness almost 4 years

    I'm currently using https://marketplace.visualstudio.com/items?itemName=mitaki28.vscode-clang which is great as a nice little tool for accessing member functions.

    I am however having one issue with a project I am importing. While the above clang feature works, I am having particular problem with using include directories. My project structure is as follows:

    |- src/
       |- main.cpp
    |- include/
       |- MyHelper.h
    |- CMakeLists.txt
    

    Is there a way to configure my include directories in Visual Studio Code such that in main.cpp I can just do: #include "MyHelper.h" instead of #include "include/MyHelper.h"?

    In the editor, it highlights my include statement saying it's unable to find the file. While the editor is not a big deal (my project compiles), the subsequent issue is the vscode-clang plugin does not work because it does not see the file.

    Perhaps even have it use the config from my CMakeLists.txt in the editor for necessary includes?

    Thanks!

  • Prometheus
    Prometheus about 7 years
    I've had the same problem on my side, and when I tried your answer, I am getting another error like following: Failed to parse "d:\Users\orcuny\Dropbox\HeyHoh\.vscode\c_cpp_properties.jso‌​n": Unexpected token / in JSON at position 66
  • kyb
    kyb about 5 years
    add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON) does not work but cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON works fine. Also set( CMAKE_EXPORT_COMPILE_COMMANDS ON ) works.
  • D.Deriso
    D.Deriso over 2 years
    Squiggles went away after following instructions above AND ensuring that my settings included "C_Cpp.intelliSenseEngine": "Tag Parser"
  • allo
    allo almost 2 years
    Is there a way to make vscode automatically look for the compile_commands.json file in the build directory?