How to integrate the "boost" library into the clangd LSP server?

After downloading the boost library via Homebrew, I can’t integrate the library itself into the linter in clangd.

Linter constantly swears that there is no such file, that is, he does not see the boost library in its entirety.

My system:

  • MacOS Ventura 13.1
  • x86_64 system

How I tried to integrate, but without much success:

  1. I checked where the root folder is located, into which the boosty library was downloaded
$ brew info boost
=> boost: stable 1.81.0 (bottled), HEAD
Collection of portable C++ source libraries
https://www.boost.org/
/usr/local/Cellar/boost/1.81.0 (15,831 files, 482.1MB) *
...
  1. Check if clangd is installed:
$ which clangd
/usr/bin/clangd
  1. Recorded the necessary parameters in the LSP server configuration file:
...
require("lspconfig").clangd.setup {
    on_attach = on_attach,
    capabilities = capabilities,
    cmd = {
        "clangd",
        "-I/usr/local/Cellar/boost/1.81.0/include",
        "-L/usr/local/Cellar/boost/1.81.0/lib"
    }
}
...

Ultimately, the LSP server stops responding to the command, and nvim writes the following: Client 1 quit with exit code 1 and signal 0

I’ll make a reservation right away that I’m new to this and scouring the Internet did not find a question for my answer.

I also faced a similar problem when integrating inputPath (i.e. any include or libruary) for linter validation. In general, my problem was solved by one discussion on GitHub.

The solution to your problem is that clangd requires you not to integrate the include/libruary command via -I<path> or -L<path>, but to create your own configuration json or txt file (it all depends on your needs, just txt format takes a simple sequence of arguments), all according to the clangd documentation (project setup).

If you want to constantly use include/libruary for linter auto-substitution, then you should include the same configuration file. To do this, clangd has a special option -compile-commands-dir=<path_to_dir_with_json_or_txt_file>, which in turn will include the configuration add-ons you need.


In your case it would look like this:

  1. We create a configuration file compile_flags.txt, which will be located at some <some_path> path.
  2. In the file compile_flags.txt, write the configuration lines you need:
-I/usr/local/Cellar/boost/1.81.0/include
-L/usr/local/Cellar/boost/1.81.0/lib
  1. Add this file to your LSP server configuration with the -compile-commands-dir=<path_to_dir_with_json_or_txt_file> option:
...
require("lspconfig").clangd.setup {
    on_attach = on_attach,
    capabilities = capabilities,
    cmd = {
        "clangd",
        "-compile-commands-dir=<some_path>"
    }
}
...

Linter should now stop cursing! You can also include any other includes/libraries you need.