-
Notifications
You must be signed in to change notification settings - Fork 1
Dev Containers
The Dev Containers extension allows us to use a docker container as a full-featured development environment. The source code can either be mounted into the container from the local filesystem or copied or cloned into it once it is running.
To install/enable the extension, follow the same steps as in the previous sections.
We also install the Docker extension (although it is not strictly necessary).
In order to use the Dev containers extension, we have to build a docker container beforehand or at least provide a Dockerfile.
For our toy repository, we use the following Dockerfile based on Ubuntu 24.04.
The container can be built by running
cd moderncpp_vscode.src/vscode
docker build -t moderncpp:vscode .from the terminal.
To take a look around in the container, execute
docker run -it --rm moderncpp:vscodeThe devcontainer.json file can be used to specify
- the container image,
- the VS Code extensions for the container,
- the VS Code settings for the container,
- how to mount/copy/clone the workspace and
- various other things.
Create a new directory .devcontainer in your local workspace and copy the devcontainer.json file:
mkdir .devcontainer
cp moderncpp_vscode.src/vscode/devcontainer.json .devcontainer/
You should not have to adjust any settings there.
Let's take a look at the most important ones:
-
"image": "moderncpp:vscode": The container image. We use the one that we have built in the previous step. -
"customizations": { "vscode": { "extensions": [ ... ] } }: The VS Code extensions that we want to use in the container. -
"customizations": { "vscode": { "settings": { ... } } }: The VS Code settings for the container. We can mostly use the local user settings except for some adjustments to path variables. -
"workspaceMount": "source=${localWorkspaceFolder},target=/home/moderncpp/moderncpp_vscode,type=bind": Specifies that we want to mount our local workspace folder in the container at/home/moderncpp/moderncpp_vscode. Since we mount the workspace, any changes we make to our workspace in the container will also apply to our local workspace. -
"workspaceFolder": "/home/moderncpp/moderncpp_vscode": Workspace folder that should open when connecting to the container.
Now we are ready to start our development container.
- Open your local workspace folder
moderncpp_vscode(if it is not opened already) - Run
>Dev Containers: Reopen in Container
If successful, you should see something like
in the bottom left corner.
To check/change the VS Code container settings,
- Run
>Preferences: Open Remote Settings (JSON) (Dev Container: vscode @ desktop-linux)
To install/enable/disable extensions in the container,
- Go to
Extensionsin the activity bar
Before running CMake, we have to comment out our project settings in .vscode/settings.json since they are only valid for the local machine:
Then we can follow the same steps as in CMake Tools.
CMake Tools should now configure your project and create a new build directory (./build.Clang-18.1.3-Debug in our case):
Note: This is where using presets instead of kits would be an advantage. One could simply define different presets for the local and container setup.
- Run
>CMake: Build
This results in a compilation error since we are using Clang 18.1.3 which does not support std::print yet.
To fix this, we simply fall back to std::cout:
- Open
./examples/ex3.cpp - Change its content to
#include <iostream>
int main() {
std::cout<< "Hello world!" << std::endl;
}- Run
>CMake: Buildagain
Now everything should compile and you can run tests and examples just like you would locally:
To check that clangd works as expected, we open again ./src/linalg.cpp and hover over nda::matrix.
clangd should give us some information on the matrix type:
To debug our code in the container with CodeLLDB, we just have to add a new configuration to the launch.json file in the .vscode folder:
Then we can follow the steps described in the Run the debugger section:
- Click on
Run and Debugin the activity bar - Click the green play icon next to
Debug ex2 devcont - Start debugging
On macOS, using CMake Tools in combination with Microsoft's C/C++ extension in a container for debugging purposes seems to be not so straightforward (at least on the author's machine).
This might be different on your machine and you can just try to follow the same steps as explained on the debugging page.