Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 927
Deftness Avatar asked Mar 21 '16 02:03

Deftness


People also ask

How do I add the include path in Visual Studio?

Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > C/C++ > General property page. Modify the Additional Include Directories property.

What is include path?

includePath An include path is a folder that contains header files (such as #include "myHeaderFile. h" ) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files.

How do I change the compiler path in VS Code?

In the Windows search bar, type 'settings' to open your Windows Settings. Search for Edit environment variables for your account. Choose the Path variable in your User variables and then select Edit. Select New and add the Mingw-w64 destination folder path to the system path.


2 Answers

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

like image 111
r0n9 Avatar answered Oct 20 '22 01:10

r0n9


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
    ],
    
like image 35
Deftness Avatar answered Oct 20 '22 01:10

Deftness