14 Jun 2024 · 3 min read

VSCode DevContainer setup for C/C++ programmers

This article delves into getting a VS Code DevContainer Development environment based setup for early C/C++ programmer. The environment runs on Debian, and hence is a good place to start for all School / College students too.

As part of my investigations for my college teaching environments, I was in a situation where I needed to teach C++. And, as some of my readers know, I am compulsively obsesses with not installing any compiler, or programming environment, on my local machine. It has to run in a throwaway-able environment.

So, I looked at the default DevContainer setup that Microsoft ships as part of their images. The environment created a 2GB image on my machine. That was really not something I like working with. So, here are the things you to be more efficient, and nimble.

Prerequisites

This article assumes you have the following already installed and configured:

Getting started

Create an empty project folder in a location of your choice on the machine. And then:

mkdir .devcontainer

This will create a .devcontainer folder inside that. The below files will go inside this folder.

The Dockerfile

I used the following Dockerfile for the environment:

FROM debian:stable-slim

RUN apt-get update \
    && apt-get install -y git g++ gcc make gdb \
    && apt-get clean

WORKDIR /root

CMD ["sleep infinity"]

This gets us in a good place. It has support for the tools mentioned in the install line, and generates an image the size of approximately 820MB. A far cry from the 2GB+ from the Microsoft Container repository. Plus, I have control over the linux version, and more.

The DevContainer

The Dockerfile is never enough. It has to be supplemented with an appropriate devcontainer.json to be effective. So, here is the my version of that file.

{
    "name": "cpp-dev-container",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "customizations": {
        "vscode": {
            "settings": {
                "remote.downloadExtensionsLocally": true,
                "telemetry.enableTelemetry": false,
                "extensions.ignoreRecommendations": false,
                "workbench.remoteIndicator.showExtensionRecommendations": false
            },
            "extensions": [
                "ms-vscode.cpptools",
                "kunalg.library-documentation-cpp",
                "danielpinto8zz6.c-cpp-compile-run"
            ]
        }
    }
}

There you go. Now you are ready to Ctrl+P or Cmd+P and Reopen in Container .

When you open the container, there is an extension called CompileAndRun that will allow you to run the current C/C++ file using the default settings. You can also set the breakpoints.

Have fun C++ing