Deftness
Deftness

Reputation: 315

Visual Studio Code: C++ include path

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!

Upvotes: 16

Views: 64775

Answers (3)

user11074381
user11074381

Reputation:

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"
}

Upvotes: 1

r0n9
r0n9

Reputation: 2759

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

Upvotes: 35

Deftness
Deftness

Reputation: 315

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

Upvotes: 6

Related Questions