reddit / emacs

Lsp-mode when I can't generate compile_commands.json

zachatttack96 · 2022-09-28 17:15
The C codebase I'm working on has a very complex build system and it can only build it in windows :(. Everyone on my team uses vs-code because the C/C++ extension intellisense works well since it just automatically indexes everything in your project directory. Is there anyway I can do this with emacs-lsp mode, is there a way to get clangd to behave like how the vscode C/C++ extension does?
Lucius_Martius · 2022-09-29 04:05
clangd does not just index projects, it builds a code model. The way C/C++ projects are structured an IDE can not really *understand* the code without some meta information (include files, defines, etc.) that is usually supplied by the buildsystem files or other metadata. What buildsystem does your project use? Maybe there is some way to extract a compile_commands.json file from it. You can look at [bear](https://github.com/rizsotto/Bear) for an alternative to generate the file. I don't know what vs-code does exactly but if it doesn't parse the info from the buildsystem in some way and really just blanket indexes all files in the project, the equivalent on emacs would be tools like ctags and etags, but don't ask me, I exclusively use lsp/clangd these days.
zachatttack96 · 2022-09-29 04:15
The project I'm working uses nmake (windows flavor of make) and the visual studio 2013 compiler, but unfortunately bear is not supported on windows. I may be stuck using ctags/etags for this project.
Lucius_Martius · 2022-09-29 04:30
>bear is not supported on windows There is also [this tool](https://github.com/nickdiego/compiledb) which claims to support Windows. However: >The project I'm working uses nmake (windows flavor of make) and the visual studio 2013 compiler Since the VS c compiler uses entirely different options compared to gcc/clang, I would be surprised if a compile_commands.json would even work if you managed to generate one. You'd need some hack that translates the options given to the VS compiler to gcc/clang commands. I've done something like that in the past to work with projects for Keil's c51, but it wasn't reliable or pretty. Honestly, in your situation I'd probably just use VS Code when working on that project and shower afterwards to no longer feel dirty.
Additional-Boot-2434 · 2022-09-29 17:38
I once had some success by providing a `compile_flags.txt` - at least I was able to use clangd and lsp-mode to some extend.
zachatttack96 · 2022-09-29 18:56
I believe I have to have a -I flag for every include directory then, would be a lot of work to account for them all, I suppose I could attempt to write a script to grab every folder that contains a .h files
whatacold · 2022-09-30 13:33
I'm not sure how the output of nmake looks like, maybe you can try this tiny web app of mine to generate `compile_commands.json` from the output: https://texttoolkit.com/compilation-database-generator And then you can use clangd with the compile_commands.json
hangingpawns · 2022-10-03 05:05
I use bear... What other options are there besides bear? I was surprised to see you lost bear as "an alternative" because I thought it was the only option.
Lucius_Martius · 2022-10-03 08:07
I meant as an alternative to the build-system generating the file without any external tools, which modern buildsystems (like CMake or Meson) usually can do for you. However, one Post down I linked [this tool](https://github.com/nickdiego/compiledb) that claims to essentially do the same as bear, with supposedly some advantages. I haven't tried it myself, I use CMake for all my projects.