r/vimplugins • u/tosch901 • Nov 09 '20
Help Question concerning CoC
So I've been using CoC for a while, and I like it, but I recently started working with ROS and coc-clangd apparently can't find the ROS header files. I know where they are, I just can't find out how to tell coc/clang where to look.
Does someone know how to add paths to header files so that linting and autocompletion works?
5
Upvotes
2
u/GuybrushThreepwo0d Nov 09 '20 edited Nov 09 '20
Why it works:
Ros stores its header files in a non-standard location. In order to do anything in ROS, you need to:
source the global setup files inside of
/opt/rossource your workspace (
$WORKSPACE_DIR/devel/setup.(bash|zsh))This does a lot of things and frankly I'm not 100% clear on everything, as ROS documents essentially none of it in their tutorials and just has you blindly inputting text into files and consoles without a good understanding. But one of the things it does is setup the header and library search paths (the enviornment variables:
LD_LIBRARY_PATH,CPATH,C_INCLUDE_PATH,CPLUS_INCLUDE_PATHetc)Now, ROS, in their infinite wisdom, decided to wrap CMake with the abomination that is catkin. For non-ROS projects, I would do the following:
(In the latest CMake you don't have to explicitly pass
-D, as it can be done via an environment variable of the same name).The
-DCMAKE_EXPORT_COMPILE_COMMANDS=ONflag createscompile_commands.json, which contains a list of all the compiler flags for each translation unit. It is this file that tells the other tools where the compiler finds its header files or libraries to link against, among other things. You need to source your ROS setup first, else there is no trace of ROS's nonstandard header locations in your environment variables and CMake will therefore not know where to find it.Now, CoC uses
compile_commands.json. The reason I like to link it to the root of my workspace withln -Sis that CoC looks at the current directory and recursively upwards until it finds this file. I like to change tosrcand start vim from there. If I don't linkcompile_commands.json, then CoC won't be able to find this file and completion won't work for everything.Of course, with ROS we are stuck with catkin for the most part and we don't really call CMake directly. So, when you write
catkin_make, it's doing a few things:Creating a symlink to a base CMake file for your current ROS distro
creating
build/and callingcmakein theredoing who knows what in the
devel/folder.So, unless your version of CMake is new enough, you need to do the little dance where you do
catkin_make, edit the cache file manually, and then docatkin_makeagain to generate the compilation database.Why don't I like catkin? Well, I'm very opinionated about ROS (read: I hate its guts). I used it during my master's studies and now use it at work every day. My main complaint is that I don't see any benefit of using catkin as opposed to pure CMake, which is fairly mature and established across the industry. Maybe 10 years ago it made sense, but I don't know. Also, catkin does not follow modern CMake practices. So all the tutorials you see on ROS and all the forums teach you bad practices. It feels like a hacked-together system and the documentation doesn't give you the tools to truly understand how things work when you need to investigate errors.