0

I am trying to configure lsp-mode with clangd using emacs 28.1 (compiled from source), and encounter two problems that I do not manage to solve:

First, `lsp-mode` is not started automatically when I open a c++ source file. The loaded major-mode is shown as 'c++-mode', and I think I have installed the corresponding hook (see below).

When I start lsp-mode with M-x lsp, it becomes active and is 'there'. What am I missing in the configuration ?

UPDATE: solved by adding (add-hook 'prog-mode-hook 'lsp-deferred)

(use-package lsp-mode
  :commands (lsp lsp-deferred)
  :hook ((c-mode-hook . lsp-deferred)
         (c++-mode-hook . lsp-deferred)
         (lsp-mode . lsp-enable-which-key-integration))
  :init
  (setq lsp-keymap-prefix "C-c l") 
  :config
  (lsp-enable-which-key-integration t))

Second, clangd does not find include files, for example <vector>. The message I get is 'vector' file not found [pp file not found].

In *clangd::stderr*, I see:

I[14:46:03.318] Ubuntu clangd version 12.0.0-3ubuntu1~20.04.5
I[14:46:03.318] PID: 73004
I[14:46:03.318] Working directory: xxxx
I[14:46:03.318] argv[0]: /usr/bin/clangd-12
I[14:46:03.318] argv[1]: --header-insertion-decorators=0
I[14:46:03.318] Starting LSP over stdin/stdout
I[14:46:03.318] <-- initialize(1)
I[14:46:03.319] --> reply:initialize(1) 0 ms
I[14:46:03.380] <-- initialized
I[14:46:03.381] <-- textDocument/didOpen
I[14:46:03.381] Loaded compilation database from /xxx/src/compile_commands.json
I[14:46:03.381] --> window/workDoneProgress/create(0)
I[14:46:03.381] Enqueueing 16 commands for indexing
I[14:46:03.382] ASTWorker building file /xxx/xxx.cpp version 0 with command 
[/xxx/build]
/usr/bin/c++ --driver-mode=g++ -DBOOST_ALL_NO_LIB -DBOOST_IOSTREAMS_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -isystem /usr/local/include/opencv4 -pthread -std=gnu++17 -o CMakeFiles/xxx.dir/src/xxx.cpp.o -c /xxx/src/xxx.cpp -fsyntax-only -resource-dir=/usr/lib/llvm-12/lib/clang/12.0.0

My project is built using cmake, and the generated compile_commands.json file in the source directory seems to be found by clangd. Yet, it does not find the right headers (which are installed and visible to g++, the project compiles just fine when I run make).

What am I missing here?

user52366
  • 131
  • 5

1 Answers1

1

The problem was that clang++ apparently looks for the "higher gcc version", and then expects that the corresponding g++ libraries and headers are installed. For some reasons, this was not the case on my system.

Trying to compile a simple c++-file (clang++ -std=c++17 -v main.cpp -o test) showed:

clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11

Running sudo apt-get install g++-11 installed the missing headers.

user52366
  • 131
  • 5