16

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!

3

3 Answers 3

36

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Squiggles went away after following instructions above AND ensuring that my settings included "C_Cpp.intelliSenseEngine": "Tag Parser"
Is there a way to make vscode automatically look for the compile_commands.json file in the build directory?
You may need to restart VSCode after modifying c_cpp_properties.json (inserting compileCommands).
6

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
    ],
    

1 Comment

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.json": Unexpected token / in JSON at position 66
1

I don't know if I'm late. I add arg in tasks.json file. In fact, same as first answer, but in vscode, we can do it easier.

In C++, use g++ -g foo.cpp -o foo -I /path/to/include/dir to add headers files.

As we know, in vscode, tasks.json is using to run bash command, but can use some alias like ${fileDirname} for, you know, file dir name:)

Anyway, task.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/out/${fileBasenameNoExtension}",
                "-I",
                "${fileDirname}/../Include/"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.