How do you create a custom intellisense database?

Marcel Pi 0 Reputation points
2023-04-18T21:39:26.14+00:00

Hi,
I have a custom build system which can generate compile_commands.json for clangd, but I would also like to have it work with Visual Studio 2022 since it has much better debugging capabilities than Code. That is, I would like to open a folder with VS and after running the build have intellisense available, like a CMake project essentially. Are there any samples or documentation on how to do this? I assume this involves generating parts of the .vs folder manually.

Developer technologies Visual Studio Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Konstantinos Passadis 19,586 Reputation points MVP
    2023-04-18T21:45:06.8366667+00:00

    Hello @Marcel Pi ! Yes, it is possible to generate Visual Studio 2022 project files from a custom build system that produces a compile_commands.json file. You can use the CMakeSettings.json file to configure the Visual Studio project to use the compile_commands.json file and other settings required by your custom build system. Here is an example CMakeSettings.json file that you can use as a starting point:

    {
      "configurations": [
        {
          "name": "My Build",
          "generator": "Visual Studio 17 2022",
          "configurationType": "Debug",
          "buildRoot": "${projectDir}/build/${name}",
          "cmakeCommandArgs": "",
          "buildCommandArgs": "",
          "variables": [
            {
              "name": "CMAKE_EXPORT_COMPILE_COMMANDS",
              "value": "ON"
            },
            {
              "name": "MY_CUSTOM_BUILD_SYSTEM_FLAG",
              "value": "ON"
            }
          ],
          "cmakeSettings": {
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/14.30.30704/bin/Hostx64/x64/cl.exe",
            "configurationType": "Debug",
            "compilerArgs": ["/Zi", "/MTd", "/Od", "/std:c++17"],
            "linkerArgs": ["/debug:fastlink"],
            "includePath": ["${workspaceRoot}/include"],
            "defines": ["MY_CUSTOM_DEFINE"],
            "browse": {
              "path": ["${workspaceRoot}/include"],
              "limitSymbolsToIncludedHeaders": true,
              "databaseFilename": "${workspaceRoot}/build/${name}/compile_commands.json"
            }
          }
        }
      ]
    }
    

    Here, the name field is the name of the configuration, the generator field specifies the version of Visual Studio to use, and the configurationType field specifies whether the configuration is for Debug or Release mode. The variables field can be used to specify any additional variables required by your build system, such as flags or paths. The cmakeSettings field contains the settings that will be passed to CMake to generate the Visual Studio project files. Here, we are setting the path to the Visual C++ compiler (compilerPath), the compiler and linker arguments (compilerArgs and linkerArgs), the include path (includePath), and the preprocessor definitions (defines). Finally, the browse field specifies the path to the compile_commands.json file and the additional paths to search for header files. Once you have created the CMakeSettings.json file, you can open the folder in Visual Studio 2022 and select the configuration you created in the dropdown menu at the top of the IDE. You should then be able to build and debug your project as usual. ALSO here is a relevant link : https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170 In case this helped kindly mark the answer as Accepted! BR


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.