TransWikia.com

VScode setup with ROS and auto complete

Stack Overflow Asked by DiXcipuli on November 4, 2021

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.

3 Answers

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.

Answered by LoW on November 4, 2021

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.

Answered by avneesh mishra on November 4, 2021

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

Answered by Richard on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP