VScode setup with ROS and auto complete
Asked Answered
O

3

15

I am struggling to configure VSCode with ROS to have the auto-complete function. I am used to using VSCode with Qt and OpenCV and everything worked fine. For example, for OpenCV, I just edited c_cpp_propreties.json like this:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/local/include/opencv4/opencv2"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "gnu11",
        "cppStandard": "gnu++14",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

The auto-complete for OpenCV works fine then (I have the C++ IntelliSense extension). But then, as soon as I try to specify the include folder from ROS in the c_cpp_propreties.json, nothing works anymore, not even the OpenCV auto-completion:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/local/include/opencv4/opencv2",
            "/opt/ros/melodic/include"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "gnu11",
        "cppStandard": "gnu++14",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

It's like ROS is blocking everything. What am I doing wrong? I specify that I click on "Open Folder" in VSCode and browse for the ROS package to load it. I am working on Ubuntu 18.04 LTS.

Old answered 15/6, 2020 at 7:23 Comment(1)
Do you have any news on this? I am also trying to get this to work? The ROS extension doesn't quite cut it.Sassafras
T
4

I had a problem when using the IntelliSense with ROS. It would simply not search the ros namespace, no matter what I did. So I thought of changing the cppStandard (in file c_cpp_properties.json) to gnu++11 (it's only for IntelliSense anyways). It immediately started working.

Here are the results with cppStandard set to gnu++14 enter image description here

Here are the results with cppStandard set to gnu++11 enter image description here

I have even tried gnu++17, but that too gave limited success. Looks like gnu++11 worked best.

I also tried what Richard had to say in his answer. It works without having to change anything else.

Taratarabar answered 26/10, 2020 at 18:58 Comment(1)
you are wizard dude, honestly, this is black magic :PCorticosterone
Q
5

You need to generate compile_commands.json and put that in your vscode work directory. This file will tell vscode linter where to look. This needs to be regenerated if you create new files or your compile instruction in CMakeLists.txt changes (i.e new libraries)

I made a youtube video to demo this

I also answered this in a similar post on a reddit: Has anyone got Visual Studio to work properly with ROS?. I've added the answer i wrote for that below;

If you use catkin_make, when you compile,

cakin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1

A compile_commands.json will then appear in your build folder after compiling.

If you use catkin tools, because the build system is different, you will have a run a script after building to merge the individual compile_commands.json into one

See the issue here for script.

You can then restart VScode, and it will prompt you and ask if you want to use it when it starts. If it didnt prompt you for whatever reason,

In the "Advanced" section of the Configuration UI, you can supply the path to your compile_commands.json and the extension will use the compilation information listed in that file to configure IntelliSense.

Basically, the steps are:

  1. cakin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=1
  2. move compile_commands from catkin_ws/build under your vscode work directory, i usually have it under catkin_ws/src, personal preference
  3. Point vscode to your compile commands.

ROS should work properly from here.

Edit: added steps from answer made in another post

Quadruplex answered 15/7, 2020 at 18:15 Comment(5)
A link to a solution is welcome, but please ensure your answer is useful without it. Answers that are little more than a link may be deleted.Manslayer
Hi. If I have let's say ros::NodeHandle n; and then I would like my autocomplete to n. find my n related methods, attributes etc. what should I do ? I mention that I'm using VScode on ubuntu and I've installed autocomplete plugin, and also I've installed ROS plugin. For the moment I only get n. -> no suggestions.Annelieseannelise
Also, what is Point vscode to your compile commands. this ?Annelieseannelise
if you followed the steps shown in the video, you should have autocomplete to n.Quadruplex
Point vscode to your compile command refers to doing you can then restart VScode, and it will prompt you...Quadruplex
T
4

I had a problem when using the IntelliSense with ROS. It would simply not search the ros namespace, no matter what I did. So I thought of changing the cppStandard (in file c_cpp_properties.json) to gnu++11 (it's only for IntelliSense anyways). It immediately started working.

Here are the results with cppStandard set to gnu++14 enter image description here

Here are the results with cppStandard set to gnu++11 enter image description here

I have even tried gnu++17, but that too gave limited success. Looks like gnu++11 worked best.

I also tried what Richard had to say in his answer. It works without having to change anything else.

Taratarabar answered 26/10, 2020 at 18:58 Comment(1)
you are wizard dude, honestly, this is black magic :PCorticosterone
P
1

In the file .vscode/c_cpp_properties.json (generated by the VSCode ROS extension), try to change from "cppStandard": "gnu++14" to "cppStandard": "c++14".

ROS is using the c++14 standard, so specifying gnu++14 seems to break things. This was deduced from this question.

An issue exists about this.

Pachton answered 8/12, 2020 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.