Dumb-Ptr · 2022-03-02 14:32
Hi, I want to set up my personal development environment for C++ in emacs, and I have a few questions:
I installed clangd and LSP, and if I open just one .cpp file it's all good, but when I bring in multiple headers and files in different directories it doesn't know where to find them (of course), how can I fix it? Also, I used to use makefiles, but I read that since clangd uses something called compile\_commands.json and CMake can produce them, the latter would be a better option... the problem is that I don't know how this whole interaction works and I can't find a clear explanation about this on the internet. Lastly, is there a way to use only makefiles instead of CMake, or is it either impossible or impractical?
aaronemassey · 2022-03-02 15:23
Idk about make but Cmake has options for producing compilation database JSON files.
So one very neat tool is Bear. In my experience Bear can work with just about any build system and creates the compilation database for ya.
It works by intercepting commands and other magic idk.
If your compilation db is not located in the project root you may have to tell clangd where to find it.
chi91 · 2022-03-02 16:47
There is a tool named ```bear``` that you invoke with a regular ```make``` and it will create your ```compile_cemmands.json```.
LorenzoFero · 2022-03-02 17:10
With bear is dead simple: install it, write your makefile and then instead of invoking make, type bear -- make, et voila! You'll get the compile_commands.json and clangd will work as expected
jjgarciaripoll · 2022-03-02 19:14
If you develop c++ you should use CMake. It handles many aspect of the language such as library dependencies, static checks, etc, very graciously. In that case you can use this https://github.com/juanjosegarciaripoll/project-cmake to handle both the project and clangd
Dumb-Ptr · 2022-03-02 19:26
so after the first build emacs will recognise headers in different directories for example?
diagonal-d · 2022-03-03 08:53
There is also compiledb [https://github.com/nickdiego/compiledb](https://github.com/nickdiego/compiledb) that works directly with your make file to generate the compile\_commands.json.
markasoftware · 2022-03-03 18:25
For small projects a Makefile is still more suitable IMO thanks to its simplicity.
LorenzoFero · 2022-03-03 18:56
Yes, however you have to use bear -- make everytime you'd normally use make, since you have to keep the .json file updated.
jjgarciaripoll · 2022-03-03 19:20
To be fair, CMakefiles can be two-liners and solve dependencies outstandingly. Is there anything simpler than this three liner?
````
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(myproject myproject.cc)
````
Just compiling the executable takes two lines of a Makefile, and more if you wish to clean up (and you need to know where the object files are placed, how many, etc.). In contrast, the CMakeLists.txt will take care of detecting the right compiler, hiding the details of what flags select the right standard, take care of optimizations depending on the type of build, adding `clang-tidy` phases if you wish to, precompiled headers, etc, Each extra feature is one or two lines. If you want to grow this way with Makefiles, you would have to migrate your Makefile to autoconf or implement all that volatile knowledge yourself.