emacs-lsp / emacs-ccls

Emacs client for ccls, a C/C++ language server
200 stars 29 forks source link

compile commands is not working in lsp-mode(ccls) #50

Closed lucaspeixotot closed 5 years ago

lucaspeixotot commented 5 years ago

Hello, I'm trying to use for the first time lsp-mode for my C/C++ projects. This is my minimal(I think) configuration for this.

2019-07-06-001628_448x629_scrot 2019-07-06-001638_388x112_scrot

When I did open the first project some functions definitions were being recognized and etc, but I was with a problem related to some libraries that were not being found. In order to solve this I read this link https://github.com/MaskRay/ccls/wiki/lsp-mode and compiled my project to generate the compile_commands.json. After that, everything seems don't work, flycheck, autocomplete and etc, and this msg(LSP :: not indexed) is being shown in every line.

2019-07-06-002338_492x453_scrot

How can I solve this and make lsp-mode work for me? Do I need to write something else in my config file? Just to be clear, I generated compile_commands.json with mkdir build && cd build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES .. && ln -s compile_commands.json.

I posted this in reddit and the people tell me that the problem was with ccls and I should ask here what can be happening.

MaskRay commented 5 years ago

2019-07-06-001628_448x629_scrot 2019-07-06-001638_388x112_scrot

Please paste text, not images.

mkdir build && cd build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES .. && ln -s compile_commands.json

Tip:

cmake -H. -Bbuild -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

to avoid cd

Here are some things you should try before filing a bug report:

If none of those help, remove this section and fill out the four sections in the template below.


Observed behavior

Describe what happened. Any aids you can include (that you think could be relevant) are a tremendous help.

Expected behavior

Describe what you expected to happen.

Steps to reproduce

  1. Select these example steps,
  2. Delete them,
  3. And replace them with precise steps to reproduce your issue.

System information

cosven commented 4 years ago

I've met the exact same phenomenon, and I solved it by adjusting the compiler flags.

TL;DR, I find the lsp server will work as long as the project can be built with clang(note: working with gcc does not state that the project will work with clang).


long long story below

I record my experience here in case people may meet the same problem. Note, I'm new to c++ and c++ build system.

My System Environment

I have a system(centos 7.6), which installed devtoolset-7(gcc 7). I also compile and install clang 11 by myself.

My Project Info

I try to use ccls on https://github.com/tikv/titan project.

After I run

cmake -H. -Bbuild -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
ln -sf build/compile_commands.json .

I can see the compiler_commands.json, and the first several lines looks like this

☁ ~/p/titan master* > head -6 compile_commands.json
[
{
  "directory": "/root/xxx/titan/build",
  "command": "/opt/rh/devtoolset-7/root/usr/bin/c++  .............. -W -Wextra -Wall -Wsign-compare -Wshadow -Wno-unused-parameter -Wno-unused-variable -Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers -Wno-strict-aliasing -std=c++11 -fno-omit-frame-pointer -momit-leaf-frame-pointer -march=native -Werror -fno-builtin-memcmp   -o CMakeFiles/titan_blob_file_iterator_test.dir/src/blob_file_iterator_test.cc.o -c /root/xxx/titan/src/blob_file_iterator_test.cc",
  "file": "/root/xxx/titan/src/blob_file_iterator_test.cc"
},

And I try to start my Emacs and open the project, but it does not work, and the lsp server(ccls) always responds with not indexed. And if I remove the compiler_commands.json, it works partially(some header file can't be found by lsp server).

I'm pretty sure that there are some problems in my json file, however, I do not know where the problem is (I think the root cause is that I do not know the working mechanism of ccls and I have little knowledge about c++).

After long time searching, I decide to try clangd to see if it can work properly, but of course, it failed. And the error message I found from the clangd:: stderr is Couldn't build compiler instance.

I search the error message in clangd source code and I found the reason this happens is that the compiler should be clang instead of gcc. So I disable my gcc and try to re-build my project with clang, to my surprise, the build-process failed. After some investigation, I adjust some compile flags and the project can be built with clang.

cmake -H. -Bbuild -DCMAKE_EXPORT_COMPILE_COMMANDS=ON  -DPORTABLE=1

After that, the lsp server also works 🙈.

I change the lsp server from clangd to ccls, and it also works well.