Setting up VSCode#
Visual Studio Code is probably the most popular text editor as of writing this guide. So, in this guide, we’ll explore how to set up VS Code with the CMR Superproject, with code IntelliSense and autocomplete.
Before proceeding, you must have set up the Superproject locally and successfully run cmr make. This will set up your Docker container and make sure all dependencies are acquired, which you’ll need to have ready before setting up VS Code.
Warning
This guide has not been tested on a fresh install of VS Code. If any step fails, contact Faizaan Datoo on Slack since he was able to set it up successfully. This warning must be removed once these steps are confirmed to work.
Using VS Code in a Docker Container#
Before we do anything else, we must install the Remote - Containers extension. By default, you can modify code directly in the Superproject directory on your host machine and all changes will propagate to the Docker image automatically, so you might be wondering why we wouldn’t just open the project directly in VS Code. Indeed, you could do this, but then you wouldn’t be able to take advantage of IntelliSense (error-catching being the most important thing here) and autocomplete. This is because all of the ROS headers and other dependencies live in the Docker container, and to access those from VS Code, you’ll need to initiate a remote session.
Once you’ve installed this extension, you’ll see an icon in the sidebar called “Remote Explorer.” In this tab, you’ll see a pane called “Containers,” and if your cmr make succeeded, you’ll see an entry called cornellmarsrover/rover here. Hover over it, and click “Attach to Container.”
After some automatic setup, you’ll be prompted to select which directory to open within the container. Select /Superproject/ and hit ‘OK’. Now, you’re running VS Code inside your Docker container!
Installing C/C++ and ROS Extensions#
Now that we’re running VS Code inside our container, install the C/C++ extension and the ROS extension. These will enable autocomplete and IntelliSense for the Superproject, but we need to perform some additional setup before it works.
If you don’t have a .vscode directory already in the root of your Superproject, go ahead and create one. Inside this directory, create a file called c_cpp_properties.json. Paste the following into this file, and save it:
{
"configurations": [
{
"name": "Linux",
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/melodic/include/**",
"/usr/include/**",
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
That will tell the C/C++ extension to search ROS Melodic and your Superproject directory for include headers. After that, you should be able to open any CPP file and be greeted with working IntelliSense! Be sure to read the Caveats section for some important information.
Caveats#
Since you’re effectively running VS Code inside the Docker image, you will not be able to use VS Code’s inbuilt Git functionality without setting up Git inside the docker container. Open up a Terminal in VS Code and run git config --global user.name "Your Name" and git config --global user.email "Your GitHub Email Address". You should be good to go after that, but if it still gives you trouble for lack of connectivity, you’ll have to use your computer’s Terminal or another Git application to perform pulls, commits and pushes.
Also, the terminal in VS Code will be running inside your Docker container, and the cmr commands assume that you are OUTSIDE the Docker container. So, you should run your cmr commands in your Terminal, rather than the VS Code terminal.