Build a linux/c++ development environment based on docker/vscode step-by-step
October 20, 2023 · 510 words · 3 min
Sometimes we want to develop some high-performance c++ application on Linux(like epoll/ioturing), installing a virtual machine is a solution, but I think it’s not the most convenient solution, today I’ll share how to build a linux/c++ development environment based on docker/vscode.
Principle
- Windows/macOs support the Docker platform with which you can run any linux-based image. So we can install docker engine and launch an image(like Ubuntu), install any linux software like a real Linux system, boost, protobuf, …
- Visual Studio Code(known as VSCode) is a powerfull IDE which has a huge number of extensions, and Microsoft provider a official solution to make Remote Development(SSH, Docker, …), so we can attach to a running container launched by Docker to do anything like a normal Linux!
Step
Let’s start to build a full-feature linux/c++ development environment from scratch!
Install Docker
Download Docker from https://www.docker.com/get-started/ base on your OS.
Install docker and launch it
Open a terminal(linux/macos) or PowerShell/CMD(Windows)
Execute the following command to launch a Ubuntu:22.04 image, this is the latest Ubuntu LTS Image.
This may take some time to download image, just be patient.
docker run -itd --name LinuxDevelop ubuntu:22.04
After the command return, you can see a hash, then go to step 6.
Execute the following command to attach in the Ubuntu container.
docker exec -it LinuxDevelop /bin/bash
After attached to the container, execute the following command to install essential software.
apt-get update apt-get install build-essential cmake git vim -y
Create a C++ file with the following code, which can tell us whether the c++ works.
#include <iostream> int main() { std::cout << __cplusplus << std::endl; return 0; }
Execute the following command to compile this program.
g++ -std=c++17 test.cc
Then you can see
a.out
in your current dictory, just execute it../a.bout 201703
Congradulations!Your linux/c++ environment is working! Execute the following command to detatch from the current container. Because we need to execute docker command on our host to save this image.
Execute the following command on your Host Terminal.
docker commit LinuxDevelop linux_develop:1.0
Yes, you get a durable image of a linux/c++ development environment.
Install Visual Studio Code
Go to https://code.visualstudio.com/ download Visual Studio Code based on Your OS.
Install Visual Studio Code and launch it.
Install the Remote Development Extension.
After you have installed the extension, you can see a Remote Explorer, attach to your Ubuntu container.
Visual Studio Code will automatically download some application in your Docker container.
Install C++ Extension
Enjoy your development in this Visual Studio Code like your local development, in which you can aslo launch Terminal.
Test
Let’s build a boost-based application.
Open terminal in Visual Studio Code and run the following command.
apt-get install libboost-dev -y
Create a cpp file
main.cc
with the following code.#include <boost/log/trivial.hpp> int main(int, char *[]) { BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; return 0; }
Compile and run
g++ -std=c++17 main.cc ./a.out # output 107400