vs-code-conan
vs-code-conan copied to clipboard
configuration provider for cpptools / IntelliSense
Hi,
it would be awesome if this extension would serve as a configuration provider for cpptools / IntelliSense. Most importanly provide the include paths for all the consumed conan packages dynamically.
Docu: https://github.com/Microsoft/vscode-cpptools-api#readme Example: https://github.com/microsoft/vscode-cmake-tools/blob/develop/src/cpptools.ts
Cheers
Thank you for this input. We will have a look into it and it would be a great addon to our Plugin.
I'd say this is currently the biggest productivity blocker for me when using VS Code and Conan together. Do you know any workaround that's possible today that let's you use IntelliSense correctly with headers from Conan dependencies?
I tried a few things a while ago, also with this (no longer maintained) alternative plugin. If I remember correctly, I could partially get it to work but some manual configuration of include paths was needed and it wasn't easy to set up.
If this plugin could make that process automated and works stable for both Conan packages in the cache and editable packages, that would be a big step forward in improving the Conan experience in VS Code!
Here is one way that seems to work to get IntelliSense for Conan dependencies. It is assumed that you build manually using conan source
, conan install
, conan build
inside a folder called build
in your workspace folder. It is also assumed that you use CMake to build your project.
- In
conanfile.py
, adapt yourbuild()
function to enable the CMake optionCMAKE_EXPORT_COMPILE_COMMANDS
. E.g.:def build(self): cmake = CMake(self) cmake.definitions["CMAKE_EXPORT_COMPILE_COMMANDS"] = "ON" cmake.configure() cmake.build() cmake.test()
- Build your project. E.g.:
The filemkdir build cd build conan install .. conan build ..
build/compile_commands.json
should have been generated now. - In VS Code, press Ctrl+Shift+P and run command
C/C++: Edit Configurations (JSON)
. Add a new propertycompileCommands
to the default configuration that points to the generatedcompile_commands.json
file:
You may also want to check and adapt the other attributes in the configuration, in particular{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "windows-msvc-x64", "compileCommands": "${workspaceFolder}/build/compile_commands.json" } ], "version": 4 }
includePath
. IntelliSense will fall back to the search paths specified there when you e.g. create a new source file that is not listed incompile_commands.json
. - Save the C/C++ configuration.
You should now be able to get IntelliSense for functions/types defined in Conan dependencies.
When you create new source files or add/change Conan dependencies, make sure to rebuild your project so that compile_commands.json
gets updated. However, I haven't found an easy way to make IntelliSense reload the compile_commands.json
apart from e.g. closing and reopening the folder in VS Code.
@chausner. Currently I am using the same approach as you mention, with exporting compile_commands.json from CMake. But I agree it would be a great help to integrate cpptools into the conan plugin. It will be my next task.
CMAKE_EXPORT_COMPILE_COMMANDS
is not supported by the default Visual Studio generator on Windows. A workaround that I have found is to change the generator to Ninja which ships with Visual Studio. See https://docs.conan.io/en/latest/integrations/build_system/ninja.html.