# Is it possible to produce a (clang compatible) compile\_commands database while targeting MSVC?

**URL:** https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679
**Category:** Usage
**Tags:** os:windows, gen:vs
**Created:** [December 19, 2023, 4:46pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679 "2023-12-19T16:46:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Adnn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@Adnn](https://discourse.cmake.org/u/Adnn)
#### Post date: [December 19, 2023, 4:46pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/1 "2023-12-19T16:46:00Z")

</div>

We would like to support a Windows development workflow on Vim. As far as we know, the most convenient LSP option would be `clangd`, [since vscode-cpptools LSP is not licensed for use outside VSCode](https://github.com/microsoft/vscode-cpptools/issues/2679#issuecomment-430344078).

[As per the documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html), compile\_commands are not generated fo VS generators.  
Even if we were to use `Ninja Generators`, the compilation database would contain `cl` command lines, not clang (which makes a lot of sense).

Sadly, this prevents us from using the very valuable Clang tools for Cpp development when the projects are compiled with MSVC.

Is there currently any way to produce a compile\_commands.json file containing Clang commands, while generating a MSVC ready project?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 20, 2023, 2:31am UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/2 "2023-12-20T02:31:45Z")

</div>

CMake only understands a single compiler per language in a given build. This is unlikely to change anytime soon. Is it possible to use `clang-cl` to interpret the command lines instead? AFAIK, `clang-tidy` can read `compile_commands.json` even if it has a `gcc`-based command line as the flags are “similar enough”. I’d expect something similar for `clang-cl` and MSVC.

---

<div class="post-metadata">

### Author: ![Adnn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@Adnn](https://discourse.cmake.org/u/Adnn)
#### Post date: [December 20, 2023, 8:32am UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/3 "2023-12-20T08:32:10Z")

</div>

Thank you for your response.

It makes sense that there is a single compiler in a given build, which is aligned with the single generator approach of the configure step. On the other hand, we could argue that by introducing a variable such as `CMAKE_EXPORT_COMPILE_COMMANDS`, CMake is now showing the ability to go beyond its original design to only output build projects, since a compilation database is essentially targeted at the tooling (which is great, Cpp needs to catch up on tooling). It is also clear that currently the tooling and the compiler are not necessarily by the same vendor, so any help from CMake in this field could be a major stepping stone in advancing the tooling ecosystem.

Thank you for mentioning `clang-cl`, we were not aware of this program. Do you know if it can be used with `clangd`? Any pointers helping to complete such a configuration would be greatly appreciated!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 20, 2023, 11:11am UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/4 "2023-12-20T11:11:31Z")

</div>

> [@Adnn](#):
>
> Thank you for mentioning `clang-cl`, we were not aware of this program. Do you know if it can be used with `clangd`? Any pointers helping to complete such a configuration would be greatly appreciated!

I don’t know the extent of `clangd` support with `clang-cl`. If no one has done it yet or it isn’t yet supported, asking for support from LLVM is more likely a faster path to such support than via CMake.

Note that CMake would need to detect support for the toolchain in question and rerun the configure/generate step to compute flags as things like `CMAKE_CXX_FLAGS` can accumulate compiler-specific bits and generator expressions could change based on compiler IDs.

---

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [December 21, 2023, 10:37am UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/5 "2023-12-21T10:37:35Z")

</div>

Actually, clang-cl works pretty good in my own case of one work project (which uses clang-cl on Windows and Ninja as generator)

---

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [December 21, 2023, 10:39am UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/6 "2023-12-21T10:39:37Z")

</div>

Agreed for this feature, sometimes I want to use my Ye Olde Vim 9.2 on Windows with project generated for Visual Studio, and the YouCompleteMe don’t properly work with these projects as it requires `compile_commands.json`. I have to modify the ycm config for folder…  
So, this feature is really very good to have 😊

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 21, 2023, 7:16pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/7 "2023-12-21T19:16:47Z")

</div>

Note that the description here is a bit inaccurate as we can generate them for MSVC…if you use a `Ninja` or `Makefiles` generator. The problem with Visual Studio (and Xcode) is that the command line is not known to CMake; the IDE’s underlying build tool crafts the actual command line from the flags CMake knows about _and project settings_. Both of these generators provide ways to inject extra properties that CMake has no idea about how they affect any given source file’s compilation line. To be truly accurate, it really needs `compile_commands.json` exporters to exist in the IDEs themselves (and then CMake could use that to make them through its mechanisms).

---

<div class="post-metadata">

### Author: ![Adnn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@Adnn](https://discourse.cmake.org/u/Adnn)
#### Post date: [December 22, 2023, 12:59pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/8 "2023-12-22T12:59:55Z")

</div>

> To be truly accurate, it really needs `compile_commands.json` exporters to exist in the IDEs themselves (and then CMake could use that to make them through its mechanisms).

I might be misunderstanding this statement, but the intent of the question was to know if there was a way to generate an MSVC build (via `Visual Studio` or `Ninja` generator), while also generating a `compile_commands.json` **that would be equivalent to the compile\_commands that would be generated by the `Makefiles` generator and `Clang` as the CXX compiler**. (Because this is exactly what I have to do right now in a separate build folder just to output the compile\_commands needed by Clang tooling, and having to keep both build folders in sync is a PITA).

So I do not think that this specific question really depends on the ability to talk to any IDE (as I suppose the `Makefiles` is able to output `compile_commands.json` without any IDE involved).

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 22, 2023, 1:02pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/9 "2023-12-22T13:02:12Z")

</div>

Ah. Yeah, there’s no current support for CMake understanding more than one toolchain for a language in a build at a time. Because things like:

```cmake
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  string(APPEND CMAKE_CXX_FLAGS " -fsomething-weird")
endif ()

```

exist, CMake would need to configure twice and hope that the builds are comparable.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [December 22, 2023, 8:39pm UTC](https://discourse.cmake.org/t/is-it-possible-to-produce-a-clang-compatible-compile-commands-database-while-targeting-msvc/9679/10 "2023-12-22T20:39:44Z")

</div>

You could create a sub-build of your own project for clang and use that.
