Linking Conan Include to VS Code
Asked Answered
G

7

14

I'm currently using Conan on a C++ project using sqlite_orm as a dependency.

When using my personal include (like myClass.hpp for example) Visual Studio Code is able to provide auto-completion but with Conan's include, no auto-completion is possible.

I'm looking for a way to link the include path of Conan to my VSCode, any idea?

Grano answered 24/9, 2019 at 10:8 Comment(1)
Can you provide an example of your CMake files to show what you have tried so far?Decennium
P
28

Add the following line in your project's .vscode/c_cpp_properties.json file

"includePath": ["${workspaceFolder}/**", "~/.conan/data/**"]

Primula answered 29/2, 2020 at 0:26 Comment(9)
Works but requires a restart of VSCode.Singletree
For me it worked with vscode 1.48.0 without restarting. Thanks for thisMange
Doesn't work on windows. C:/.conan is the directory and it doesn't contain data .Roeder
Oh and ~/.conan/data doesn't contain any header files, they are all located in C:/.conan/somehash/1/includeRoeder
But adding C:/.conan/** doesn't work either. github.com/microsoft/vscode-cmake-tools/issues/1213Roeder
I'm gonna stick with VS19/CLion until this is fixed.Roeder
for windows in JSON config file for all cpp projects "C_Cpp.default.includePath": ["${workspaceFolder}/**","${env:USERPROFILE}/.conan/data/**"]Balf
With the new conan2: ~/.conan2/** this works.Delaryd
@divyanshu-kalra this is the most updated and helpful answer. I think the answer should be edited to amend with conan 2's new path.Vivien
M
15

Add set(CMAKE_EXPORT_COMPILE_COMMANDS ON) to your CMakeLists.txt (or add to cmake: cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..) so a build/compile_commands.json will be generated.

VS Code (clion, etc) can utilize this file to support auto complete:

$ cat .vscode/c_cpp_properties.json
{
    "configurations": [
    {
        "name": "Linux",
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    }
    ],
    "version": 4
}
Mcghee answered 7/1, 2022 at 5:13 Comment(1)
Thanks a lot for this, also for anyone trying this I think you should be aware that some versions may differ, so please check down your build folder for the compile_commands.json file an then specify the correct path :)Grishilde
A
3

Conan doesn't provide an extension for vscode yet, but you can try:

https://github.com/FIREFOXCYBER/conan-tools-vs-code

It's available on marketplace.

Otherwise, you can add manually the package folder path (e.g. ~/.conan/data/package/version/package/package_id/include) in your settings.

Abamp answered 24/9, 2019 at 12:8 Comment(0)
G
3

A combination of dvorak4tzx's and spanishgum's answers, combined with VS Code's option for Customizing default settings is the following option:

Generate a build/compile_commands.json eiter by adding to CMakeLists.txt:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

or on the command line:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

And then insert into .vscode/settings.json:

"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",

Note: I experienced trouble if

"configurationProvider": "ms-vscode.cmake-tools"

was part of the c_cpp_properties.json. Without it, the IntelliSense works as expected.

I would argue that "includePath": ["${workspaceFolder}/**", "~/.conan/data/**"] is not desired, as you may have many versions of each library beneath ~/.conan/data.


Sources:

Geniculate answered 30/1, 2023 at 18:22 Comment(0)
G
2

After searching in the settings of VSCode, I found that you can change the path of your include in c_cpp_properties.json file that you can find in your .vscode folder

Adding the path you want in the includePath field allow you to choose your own include path

Grano answered 24/9, 2019 at 14:35 Comment(0)
S
2

I want to add that you can also use the .vscode/settings.json file as an alternative to .vscode/c_cpp_properties.json.

For example, I just set up a project with this:

{
  "C_Cpp.clang_format_path": "/usr/lib/llvm-10/bin/clang-format",
  "C_Cpp.default.includePath": [
    "~/.conan/data/**"
  ],
}
Schistosomiasis answered 28/1, 2021 at 3:5 Comment(0)
A
0

VS Code integration with Conan is limited, but the CMake plugin is highly configurable.

In a project directory, my_project,

This is the incantation that works for me currently. The UI and plugins change often, but this works in VS Code 1.92, with CMake plugin 0.0.17. I assume you're using CMake to model your artifacts.

Andes answered 12/8 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.