vs-code-conan icon indicating copy to clipboard operation
vs-code-conan copied to clipboard

configuration provider for cpptools / IntelliSense

Open SirLemyDanger opened this issue 4 years ago • 5 comments

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

SirLemyDanger avatar Jan 14 '21 13:01 SirLemyDanger

Thank you for this input. We will have a look into it and it would be a great addon to our Plugin.

mymichu avatar Jan 17 '21 09:01 mymichu

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!

chausner avatar Feb 06 '21 15:02 chausner

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.

  1. In conanfile.py, adapt your build() function to enable the CMake option CMAKE_EXPORT_COMPILE_COMMANDS. E.g.:
    def build(self):
        cmake = CMake(self)
        cmake.definitions["CMAKE_EXPORT_COMPILE_COMMANDS"] = "ON"
        cmake.configure()
        cmake.build()
        cmake.test()
    
  2. Build your project. E.g.:
    mkdir build
    cd build
    conan install ..
    conan build ..
    
    The file build/compile_commands.json should have been generated now.
  3. In VS Code, press Ctrl+Shift+P and run command C/C++: Edit Configurations (JSON). Add a new property compileCommands to the default configuration that points to the generated compile_commands.json file:
    {
        "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
    }
    
    You may also want to check and adapt the other attributes in the configuration, in particular includePath. IntelliSense will fall back to the search paths specified there when you e.g. create a new source file that is not listed in compile_commands.json.
  4. 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 avatar Feb 06 '21 20:02 chausner

@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.

mymichu avatar Feb 08 '21 06:02 mymichu

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.

chausner avatar Feb 10 '21 17:02 chausner