ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
3

How to add ROS to path in VS Code?

asked 2017-03-09 01:56:18 -0600

updated 2017-11-23 02:11:39 -0600

Hello,

Is anyone using VS Code to program in ROS? I kind of like this IDE ( haven't used it earlier ) and want to use it as it comes with a lot of features which I like.

My question is when I include <ros/ros.h> it puts a green squiggle below it, and also for service file generated headers it can't really locate them. Any suggestions on how to add on to the path to avoid this.

To be noted, there is no problem in building it as it is already in the path of Linux terminal...

edit retag flag offensive close merge delete

Comments

1

There isn't a section for VS Code on the ROS IDE wiki, but the instructions for one of the other IDEs might provide some hints.

ahendrix gravatar image ahendrix  ( 2017-04-10 01:46:28 -0600 )edit

5 Answers

Sort by » oldest newest most voted
6

answered 2017-11-11 13:08:02 -0600

clyde gravatar image

The IDE wiki now includes an entry for VSCode, which points to a ROS extension. Among other things, this extension will add the appropriate include paths. I tried this and it works nicely.

edit flag offensive delete link more

Comments

1

If the task provided by the VSCode ROS extension does not work (in my experience that feature is brittle), you might find this answer helpful.

samarth.robo gravatar image samarth.robo  ( 2020-12-14 14:19:27 -0600 )edit
8

answered 2020-11-30 13:18:05 -0600

Diogo42 gravatar image

For ROS to work completely in Vscode, without an external plugin.

First. change "C_Cpp.intelliSenseEngine": "Tag Parser" for autocomplete: image description

in .vscode folder, make or change tasks.json for build, clean and prepare for debug.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "prerun",
            "type": "shell",
            "dependsOn": [
                "build package only"
            ],
            "command": "source ../../devel/setup.bash && export ROS_MASTER_URI=http://localhost:11311/ "
        },
        {
            "label": "build all packages",
            "type": "shell",
            "command": "cd ~/catkin_ws/ && catkin config --extend /opt/ros/noetic && catkin build -DCMAKE_BUILD_TYPE=Debug -j8",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        },
        {
            "label": "build package only",
            "type": "shell",
            "command": "cd ~/catkin_ws/ && catkin config --extend /opt/ros/noetic && catkin build ${workspaceFolderBasename} -DCMAKE_BUILD_TYPE=Debug -j8",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "cd ~/catkin_ws/ && catkin clean --yes"
        },
        {
            "label": "release",
            "type": "shell",
            "command": "sudo checkinstall --install=no catkin build -j4 --cmake--args -DCMAKE_BUILD_TYPE=Release"
        }
    ]
}

In the file c_cpp_properties.json add include files propriety

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/ros/noetic/include/**",
                "~/catkin_ws/devel/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

For debug, make ou change launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${env:HOME}/catkin_ws/devel/lib/${workspaceFolderBasename}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "prerun",
            "MIMode": "gdb",
            "targetArchitecture": "x64",
            "avoidWindowsConsoleRedirection": true,
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Final result:

image description

edit flag offensive delete link more
7

answered 2018-07-15 18:14:40 -0600

amer gravatar image

In addition to clyde answer:

1- Open VSCode and (Ctrl+Shift+X) and search for ROS

2- Install ( ROS VSCode Extension )

3- Restart VSCode

4- Make sure to open your workspace ex: file ->openfolder ->catkin_ws

5- The extension will pop up a msg to configure your ROS distro if it cants configure it automatically

6- Also you can use the terminal from VSCode (Ctrl+~) as usual ubuntu terminal

7- Build your catkin workspace at least once from this terminal

8- You can include <ros/ros.h> in your files and you shouldn't see green squiggle below if everything is configured properly.

9- At the left bottom of VSCode you will find [x ROS Master] that's mean ROS core is not running try to run $ roscore from the terminal in VSCode and notice [✔ ROS Master]

edit flag offensive delete link more

Comments

I am trying with simple talker.cpp program. I could not find a way to RUN it. I have followed all your steps. When I debug I get this following error: Unable to start debugging. Program path '/home/catkin_ws/src/beginner_tutorials/scripts/talker.cpp' is missing or invalid.

murali1842 gravatar image murali1842  ( 2018-10-26 16:23:57 -0600 )edit

GDB failed with message: "/home/catkin_ws/src/beginner_tutorials/scripts/talker.cpp": not in executable format: File format not recognized

murali1842 gravatar image murali1842  ( 2018-10-26 16:25:06 -0600 )edit

This may occur if the process's executable was changed after the process was started, such as when installing an update. Try re-launching the application or restarting the machine.

murali1842 gravatar image murali1842  ( 2018-10-26 16:25:20 -0600 )edit

Can you give me some suggestions please...

murali1842 gravatar image murali1842  ( 2018-10-26 16:25:38 -0600 )edit

Im sorry but my vscode cannot configure automatically as well as dont pop up the msg to configure ROS distro. Is there any way to fix this? Anyone helps me!

chrishuynh gravatar image chrishuynh  ( 2020-05-17 12:18:48 -0600 )edit
0

answered 2023-06-15 13:51:19 -0600

[Posting incase anyone needs help]


All these answers are fine, but this is what worked for me, and it was very easy. It is similar to amer's answer.


  • Open VSCod
  • Open your workspace ex: file ->openfolder ->catkin_ws

When you open the file you are working on it will have the squiggles. If you have the C++ extension, when you hover your mouse over the headers you should be able to "Edit "includePath" Settings" via the "Quick Fix" button. There may also be a pop up on the bottom right of your screen from intellisense to configure the file.


When you click this it will create a "c_cpp_properties.json" file in a ".vscode" directory within the workspace you are working in. The file should look like this:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "linux-gcc-x64"
    }
],
"version": 4
}

All you need to do is add your distribution path to the "includePath" section. Your code should look something like this then.

...
"includePath": [
    "${workspaceFolder}/**",
    "/opt/ros/kinetic/**"
 ],
...

Adding the " * " just indicates recursive search. For example, ${workspaceFolder}/* will search through all subdirectories while ${workspaceFolder} will not.


If your Service created .h files are also not showing up then it's probably because you aren't in the top level of your workspace, or your .h file is outside of your workspace. You can add their file path the same way as above. You can find these service .h files in the catkin_ws/devel/include/Service_Name directory of the workspace they were created in.

edit flag offensive delete link more
0

answered 2021-12-01 03:09:12 -0600

ticotico gravatar image

The easiest setup for ROS with VS Code is to install this ROS extension, which handles normally pretty much everything automatically. More information:

Cheers

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2017-03-09 01:56:18 -0600

Seen: 36,700 times

Last updated: Dec 01 '21