reddit / emacs

C++ Project File Required For Development With Clangd and LSP

samcdc6600 · 2022-04-24 08:49
Hello, I've just managed to setup LSP-Mode on emacs. I'm using Clangd and upon opening a C++ based program I've been working on and entering "lsp-find-references" I saw that it wasn't finding all references. I believe this is because LSP-Mode didn't find a project file when I opened the C++ file and I pointed it to the root folder of the project, but of course it didn't find a project file because I don't have one (why would I need something so useless when I've got a make file :) .) Anyway I tried adding a compile\_commands.json file that I generated using this web sight: [https://texttoolkit.com/compilation-database-generator](https://texttoolkit.com/compilation-database-generator) and changing the lines with "/path/to/your/project/" to point to my project. However it doesn't seem to have worked. How can I get LSP-Mode to recognize all the files in my C++ project?
Pinsl · 2022-04-24 13:05
Have a look at [Bear](https://github.com/rizsotto/Bear). Or, in case you are building your project with cmake, you can [tell it to generate the json file for you](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html).
NoisyFrequency · 2022-04-24 13:19
Same advice here, I `make clean` my project and then generate the `compile_commands.json` with `bear -- make -j8`
samcdc6600 · 2022-04-24 13:30
> bear -- make -j8 So if I need to add or delete any files I just regenerate the file using bear right?
NoisyFrequency · 2022-04-24 13:39
`bear` will use your build tools like a Makefile or CMake. It kinda wraps around the build process to get what's needed so you can have to re-build your project for it to build the proper `compile_commands.json`.
samcdc6600 · 2022-04-24 14:23
I haven't been able to get it to work yet. I'm not sure what I'm doing wrong. I've run "bear gamke" to generate the compile\_commands.json. Then when I open a file in my project it LSP isn't started and when I run lsp it asks me for the directory of the project. I tell it to look in the same directory the compile\_commands.json file is in (which is the same directory as my make file, which is one directory above all my source code files). But it doesn't seem to be working. I've done what I just described in the following order. \* I've generated the file in the directory "\~/softDev/nScroll/game" using "bear gamke" (this is in the same directory as my make file so I think the paths should be correct.) \* I've tried deleting the "\~/.emacs.d/.lsp-session-v1" file (which is why it asked me for the project directory again) \* I've run "emacs ./src/main.cpp" and then run "lsp" in the minibuffer because it wasn't running. \* I've told it that the work space is "\~/softDev/nScroll/game". But it's not working properly.
NoisyFrequency · 2022-04-24 14:27
I suppose you mean `make` ? You have to run `bear -- make` with the hyphens and the spaces.
samcdc6600 · 2022-04-24 14:34
Ahh yes sorry I mean `bear -- gmake` I'm using FreeBSD but I'm using GNU Make. Thus the use of gmake.
samcdc6600 · 2022-04-24 14:35
This is the start of the compile\_commands.json file that is generated. I don't think there's anything wrong with it. `[` `{` `"arguments": [` `"/usr/bin/clang++",` `"-c",` `"-std=c++2a",` `"-Wall",` `"-Wpedantic",` `"-Wextra",` `"-O2",` `"-o",` `"obj/checkBoundsOfBounds.o",` `"src/checkBoundsOfBounds.cpp"` `],` `"directory": "/usr/home/topaz/softDev/nScroll/game",` `"file": "/usr/home/topaz/softDev/nScroll/game/src/checkBoundsOfBounds.cpp",` `"output": "/usr/home/topaz/softDev/nScroll/game/obj/checkBoundsOfBounds.o"` `},` `{` `"arguments": [` `"/usr/bin/clang++",` `"-c",` `"-std=c++2a",` `"-Wall",` `"-Wpedantic",` `"-Wextra",` `"-O2",` `"-o",` `"obj/collapse.o",` `"src/collapse.cpp"` `],` `"directory": "/usr/home/topaz/softDev/nScroll/game",` `"file": "/usr/home/topaz/softDev/nScroll/game/src/collapse.cpp",` `"output": "/usr/home/topaz/softDev/nScroll/game/obj/collapse.o"` `},`
samcdc6600 · 2022-04-24 14:40
Also I ran `gmake clean` before I did the bear command.
NoisyFrequency · 2022-04-24 14:40
This sounds correct, the compile_commands.json should live where your Makefile is but you can also have a specific compile_commands.json per directory if you're loading external libraries for example. LSP should pick it up without issue, what's the problem after you restart the language server (`M-x lsp-workspace-restart`) ?
samcdc6600 · 2022-04-24 14:48
So if I restart the server using the command you just said to use and then I put the point over the name of a function in a function call and run `M-x lsp-find-references` It doesn't seem to return what it should. So the function is call is "loadAssets(..." and it opens another buffer and only has one reference listed (the line that point is on in the file I was just looking at and the function is defined in another file and declared in yet another file, so I think there should be at least three references.)
samcdc6600 · 2022-04-24 14:48
If that makes sense.
samcdc6600 · 2022-04-24 14:49
I've noticed some other behavior that would probably be consistent with it not now where all the files in my project are. I think it's only searching files that I already have open.
NoisyFrequency · 2022-04-24 14:50
I'm not sure I understand the issue you're having
samcdc6600 · 2022-04-24 14:55
So I'm telling it to find the references to a function and it's just returning the reference in the current file, but I know for a fact that there should be at least two other references to that function in other files.
samcdc6600 · 2022-04-24 14:59
And just to confirm I'm not going insane I just opened one of the other files and then run the `lsp-find-references` command again and now it's showing two references.
samcdc6600 · 2022-04-24 15:01
I mean I guess I could just write some elisp function that opens all the files under the current directory when run haha. But I shouldn't have to do that and if I had lots of files that wouldn't work very well.
NoisyFrequency · 2022-04-24 15:06
If you can show a minimal working example, you could open an issue on the LSP repo and get help there. Or try to emacs lsp Discord.
samcdc6600 · 2022-04-24 15:08
Hmm okay. Thanks. I might check out the discord.
yep808 · 2022-04-24 20:39
Bear seems to be the popular choice. An alternative that I use is compiledb (pip installed).
samcdc6600 · 2022-05-07 17:46
I'm not really a big Python fan. So I'll probably just use Bear. Thanks for the reply :).
samcdc6600 · 2022-05-08 10:36
I tried compiledb because bear doesn't add any information about .hpp files to the .json file it produces and I wanted to see if compiledb does, however it doesn't work because as far as I can tell it expects "make" as an argument. I'm using gmake though. Since I'm using FreeBSD make isn't the same as gmake.
samcdc6600 · 2022-05-08 10:38
I think at least part of the problem might be that I have my include files in a different directory to my .cpp files and bear doesn't put any info about the include files in the .json file and I read that clangd won't find them if there not in the same directory or if it's not told where to find them.