-
-
Save galou/92a2d05dd772778f86f2 to your computer and use it in GitHub Desktop.
| ########################################################################## | |
| # ycm_extra_conf.py for ROS # | |
| # Author: Gaël Ecorchard (2014) # | |
| # # | |
| # Place this file in your ROS catkin workspace to use it. # | |
| # # | |
| # License: CC0 # | |
| ########################################################################## | |
| def getRosIncludePaths(): | |
| import os | |
| try: | |
| from rospkg import RosPack | |
| except ImportError: | |
| return [] | |
| rospack = RosPack() | |
| includes = [] | |
| includes.append(os.path.expandvars('$ROS_WORKSPACE') + '/devel/include') | |
| for p in rospack.list(): | |
| if os.path.exists(rospack.get_path(p) + '/include'): | |
| includes.append(rospack.get_path(p) + '/include') | |
| if os.path.exists('/opt/ros/indigo/include'): | |
| includes.append('/opt/ros/indigo/include') | |
| if os.path.exists('/opt/ros/hydro/include'): | |
| includes.append('/opt/ros/hydro/include') | |
| return includes | |
| def getRosIncludeFlags(): | |
| includes = getRosIncludePaths() | |
| flags = [] | |
| for include in includes: | |
| flags.append('-isystem') | |
| flags.append(include) | |
| return flags | |
| # some default flags | |
| # for more information install clang-3.2-doc package and | |
| # check UsersManual.html | |
| flags = [ | |
| '-Wall', | |
| '-Werror', | |
| # std is required | |
| # clang won't know which language to use compiling headers | |
| '-std=c++98', | |
| # '-x' and 'c++' also required | |
| # use 'c' for C projects | |
| '-x', | |
| 'c++', | |
| # include third party libraries | |
| # '-isystem', | |
| # '/some/path/include', | |
| ] + getRosIncludeFlags() | |
| # youcompleteme is calling this function to get flags | |
| # You can also set database for flags. Check: JSONCompilationDatabase.html in | |
| # clang-3.2-doc package | |
| def FlagsForFile(filename): | |
| return { | |
| 'flags': flags, | |
| 'do_cache': True | |
| } |
Thanks for sharing this! Saved my life with c++ codes...however I was wondering how can I get similar autocompletions for Rospy files. I still do not completely understand how to configure YCM for python files. Any pointers would be great.
After I copy it to my .ycm _extra_conf.py,i find that when i write my code,the vim can't recognize the header file ,generate by catkin_make, in the catkin_ws/devel/include,but i see you have add the path.so i really don't know how to deal with it
sorry,It's my problem.I have deal with it.thanks
@ghost: any luck? - I have a similar problem:
I tried the config on the ROS tutorials, by copying it to ~/catkin_ws/.ycm_extra_conf.py, I also made sure it is executed when I open a file in vim.
When I use export ROS_WORKSPACE=~/catkin_ws/src #import ros/ros.h seems to be found (it is not highlighted), however #include "beginner_tutorials/AddTwoInts.h" is not (this is stored in ~/catkin_ws/devel/include/beginner_tutorials/).
But if I use export ROS_WORKSPACE=~/catkin_ws both #imports are highlighted, so are all uses of the ros namespace.
@schoelst: I find this problem, too! There are two functions containing $ROS_WORKSPACE, GetRosIncludePaths and GetCompilationDatabaseFolder. The first one works properly with export ROS_WORKSPACE=~/catkin_ws, but the second one doesn't.
You can check the description of GetCompilationDatabaseFolder,
Return the absolute path to the folder (NOT the file!) containing the
compile_commands.json file to use that instead of 'flags'.
So this function find the folder containing compile_commands.json and use the json file to check instead of the flags.
Thus, if we set ROS_WORKSPACE to the right path, export ROS_WORKSPACE=~/catkin_ws, it will find the build folder ~/catkin_ws/build/your_pkg but there is no compile_commands.json. So the setting of 'nothing' cover the flags and ycm have no idea how to compile it.
If we set it to the wrong path, export ROS_WORKSPACE=~/catkin_ws/src, there is no folder '~/catkin_ws/src/build/your_pkg`, so ycm just ignore it and use the flags, which contains the headers of ros.
Therefore, there are two ways to solve it.
- Just
return ' 'inGetCompilationDatabaseFolder. It works but ycm only know the header of ros, so some mistakes may happen. - Recommended Add
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )in your CMakeLists.txt to generate thecompile_commands.json. Then ycm will know exactly how to compile your project.
If set(CMAKE_EXPORT_COMPILE_COMMANDS ON) is added to package's CMakeLists.txt, compile_commands.json is still generated at ~/catkin_ws/build/ not at ~/catkin_ws/build/package/.
Do I miss anything?
@egoist-sx: I think this is the default behaviour of cmake at least the newer versions. This is my version which works for me: https://gist.github.com/woidpointer/d596b293b050b12bb1e68eaf00428fd6
Additionally, to get it completely running I have to install rospkg via pip3 (sudo pip3 install -U rospkg)
to import the referenced libs in python3.
I use catkin-tools and run catkin config -DCMAKE_EXPORT_COMPILE_COMMANDS=ON on the command line. You need to run this command just once. There is no need to add this to the CMakeLists.txt files.
@egoist-sx: I think this is the default behaviour of cmake at least the newer versions. This is my version which works for me: https://gist.github.com/woidpointer/d596b293b050b12bb1e68eaf00428fd6
Additionally, to get it completely running I have to install rospkg via pip3 (sudo pip3 install -U rospkg)
to import the referenced libs in python3.
Your .ycm_extra_conf.py worked for me. Thank you so much 👍
Hi All,
I copied the conf file to my ~/catkin_ws/ and added extra line, set( CMAKE_EXPORT_COMPILE_COMMANDS ON ), to my CMakeLists.txt. When I load vim under ~/catkin_ws/src, it works perfectly. YCM can locate both ros packages and my personal packages correctly. However, if I use vim under ~/catkin_ws/src/beginner_tutorials/src, YCM can find neither ros packages nor my personal packages. It will report file not found error for lines #include "ros/ros.h" and #include "beginner_tutorials/AddTwoInts.h".
May I ask how can I solve this?
Thanks a lot in advance
Well, it seems my issue is the same as the one discussed above. Updating .ycm_extra_conf.py solves the problem.
This was super helpful, thank you so much!
I spent a some time tweaking this today, and was able to make a few improvements. Most notably, with this version, you don't need to specify the ROS_WORKSPACE environment variable; it figures that out automatically based on your current directory. I also made it available as a vim plugin, so you can install it using Plug (or whichever plugin manager you prefer).
Code and instructions are here: https://github.com/kgreenek/vim-ros-ycm
i put .ycm_extra_conf.py under catkin_ws, i defined in my bashrc export ROS_WORKSPACE= ~/catkin_ws I have to nodes inside a package my_first_node.py and my_first_node.cpp, so in the node.py all the functions import etc is recognized without .ycm_extra_conf.py however the cpp does not even recognize the #include <ros/ros.h> can somebody help me.
@egoist-sx: I think this is the default behaviour of cmake at least the newer versions. This is my version which works for me: https://gist.github.com/woidpointer/d596b293b050b12bb1e68eaf00428fd6
Additionally, to get it completely running I have to install rospkg via pip3 (sudo pip3 install -U rospkg)
to import the referenced libs in python3.
This solution works for me. Thank you!
I realized not so long time ago that YCM supports using compile_commands.json directly. So you can try to configure with catkin config -DCMAKE_EXPORT_COMPILE_COMMANDS=ON and then symlink the compile_commands.json into the source directory of your package.
I have added a tutorial for the whole setup for YCM and updated this file to work with ROS2 too using the compile_commands.json https://github.com/Briancbn/ros_vim_autocomplete
I can include the symlink method too if needed. The extra config still has the advantage of working with .cpp and .hpp files before the first compilation.
Thanks a lot for sharing this! I find it very useful. :)