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!
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