DEV Community

Navneet Karnani
Navneet Karnani

Posted on • Originally published at blog.mandraketech.in on

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.

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"]
Enter fullscreen mode Exit fullscreen mode

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"
            ]
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

About the Author

The Author, Navneet Karnani, began coding with Java in 1997 and has been a dedicated enthusiast ever since. He strongly believes in the "Keep It Simple and Stupid" principle, incorporating this design philosophy into all the products he has developed.

Navneet works as a freelancer and is available for contracts, mentoring, and advisory roles related to technology and its application in software product development.

Additionally, Navneet serves as a visiting faculty member at FLAME University.

Driven software engineer (Java since 1997) with a hands-on passion for building impactful tech products. Possesses over 25 years of experience crafting solutions to complex business and technical challenges.

Top comments (0)