Search code examples
c++cclangcoc.nvim

I can't figure out how to make ccls work in vim on ubuntu


Here is my problem : I've installed coc plugin on vim and it work perfectly with all language except with C/C++.

The problem is with C/++, ccls don't find my system libraries... (I tried with clangd too, I had the same problem).

Here is my coc-settings.js :

{
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "rootPatterns": [".ccls", "compile_commands.json"],
      "filetypes": ["c", "cpp", "c++", "objc", "objcpp"]
    }
  }
}

Here is my compile_commands.json (compiled whit CMake, placed at my project root and my vim current directory) :

[
{
  "directory": "/media/romain/Donnees/Programmation/C++/cmake/fxplorer/build",
  "command": "/usr/bin/g++ -Dmydebug -I/media/romain/Donnees/Programmation/C++/cmake/fxplorer/src -I/media/romain/Donnees/Programmation/C++/frameworks -I/media/romain/Donnees/Programmation/C++/frameworks/files -I/media/romain/Donnees/Programmation/C++/libs/json -I/media/romain/Donnees/Programmation/C++/libs/imgui -I/media/romain/Donnees/Programmation/C++/libs/imgui/backends -I/media/romain/Donnees/Programmation/C++/cmake/fxplorer/../imgui_custom-menus/src -isystem /media/romain/Donnees/Programmation/C++/libs/boost-install/include -isystem /media/romain/Donnees/Programmation/C++/libs/glew-2.1.0/include -isystem /media/romain/Donnees/Programmation/C++/libs/glfw-3.3.7/include -isystem /media/romain/Donnees/Programmation/C++/libs/FreeImage/Source -isystem /media/romain/Donnees/Programmation/C++/libs/FreeImage/Wrapper/FreeImagePlus -g -O0 -g3 -std=gnu++17 -o CMakeFiles/fxplorer.dir/media/romain/Donnees/Programmation/C++/cmake/imgui_custom-menus/src/CustomMenus.cpp.o -c /media/romain/Donnees/Programmation/C++/cmake/imgui_custom-menus/src/CustomMenus.cpp",
  "file": "/media/romain/Donnees/Programmation/C++/cmake/imgui_custom-menus/src/CustomMenus.cpp"
},
... other stuff here ... (but can't put it because it's too long)
{
  "directory": "/media/romain/Donnees/Programmation/C++/cmake/fxplorer/build",
  "command": "/usr/bin/g++ -Dmydebug -I/media/romain/Donnees/Programmation/C++/cmake/fxplorer/src -I/media/romain/Donnees/Programmation/C++/frameworks -I/media/romain/Donnees/Programmation/C++/frameworks/files -I/media/romain/Donnees/Programmation/C++/libs/json -I/media/romain/Donnees/Programmation/C++/libs/imgui -I/media/romain/Donnees/Programmation/C++/libs/imgui/backends -I/media/romain/Donnees/Programmation/C++/cmake/fxplorer/../imgui_custom-menus/src -isystem /media/romain/Donnees/Programmation/C++/libs/boost-install/include -isystem /media/romain/Donnees/Programmation/C++/libs/glew-2.1.0/include -isystem /media/romain/Donnees/Programmation/C++/libs/glfw-3.3.7/include -isystem /media/romain/Donnees/Programmation/C++/libs/FreeImage/Source -isystem /media/romain/Donnees/Programmation/C++/libs/FreeImage/Wrapper/FreeImagePlus -g -O0 -g3 -std=gnu++17 -o CMakeFiles/fxplorer.dir/media/romain/Donnees/Programmation/C++/libs/imgui/misc/cpp/imgui_stdlib.cpp.o -c /media/romain/Donnees/Programmation/C++/libs/imgui/misc/cpp/imgui_stdlib.cpp",
  "file": "/media/romain/Donnees/Programmation/C++/libs/imgui/misc/cpp/imgui_stdlib.cpp"
}
]

And here my .ccls file (placed in the same dir that the compile_commands.json file) :

"clang": {
    "args" : ["-std=C++17"]
}

And with this config I have errors likes : 'can't find "vector" in namespace "std". I have errors like ccls don't even read the compile_commands.json file because it can't find any function/method from libraries linked in it.

I can't figure what I'm doing wrong... ?

Thanks in advance for your time.


Solution

  • I've been digging for some time now and I found the solution. I moved back on clangd instead of ccls but the steps should be the same. (ccls is based on clangd)

    I had 2 problems :

    • 1 : clangd wouldn't find my system header librairies (even with the flag -isystem in my compile_command.json generated via CMake).

    So I had to give it to it. Like these settings will be for all my projects, I decided to make this configuration global for clangd.

    For this, you need to create a file in YOUR_CONFIG_FOLDER/clangd/config.yaml (for me - Unbuntu 22 - it was ~/.config/clangd/config.yaml, you need to create the folder clangd if it does not exists yet.) After that, you need to put the path of where are your system headers and your compiler headers (for me : gcc, g++). This is the content of my file :

    CompileFlags:
      Add: [-I/usr/include, -I/usr/include/x86_64-linux-gnu, -I/media/romain/Donnees/Programmation/C++/frameworks, -I/usr/include/c++/11, -I/usr/include/x86_64-linux-gnu/c++/11, -std=c++17]
    

    For more information about this file and its format, you can visit the clangd website.

    Ok after that, the system header missing problem was gone. But like I said, I had another problem.

    For some of my project files, the weren't in a sub-folder of my project. (because the are a frameworks that a write for all my apps). I didn't want to move them.

    The problem is that when a file is not in a subfolder of your project, clangd change the project_root folder for this file and don't found the compile_command.json file anymore.

    But luckly, clangd give the possibility to create a config file per folder. So that's what I did, I created a config file for my frameworks folder.

    The method is to create a file named .clangd and to put it in the root folder of your frameworks directory.

    The .clangd file have the same syntax of the config.yaml. Here is the content of mine :

    CompileFlags:
      Add: [ 
    -I/media/romain/Donnees/Programmation/C++/libs/json,-I/media/romain/Donnees/Programmation/C++/libs/imgui,-I/media/romain/Donnees/Programmation/C++/libs/imgui/backends,-I/media/romain/Donnees/Programmation/C++/libs/glfw-3.3.7/include,-I/media/romain/Donnees/Programmation/C++/libs/FreeImage/Source,-I/media/romain/Donnees/Programmation/C++/libs/FreeImage/Wrapper/FreeImagePlus,-I/media/romain/Donnees/Programmation/C++/libs/boost-install/include,-I/media/romain/Donnees/Programmation/C++/libs/glew-2.1.0/include]
    

    I just did a copy of my cmake includes when I use my frameworks.

    And that was it, clangd work perfectly fine now. I hope this one will help you too if your C++ LSP don't work as expected.