利用Docker搭建Linux C++ Boost开发环境

简介

本文介绍如何使用Docker搭建Linux环境下C++的开发环境。众所周知C++的开发环境分为Windows和Linux两种,在windows配置C++开发环境并实现了Asio服务器的开发,我们也做过QT的配置和使用,前面的教程已经很完善了,接下来介绍如何在Linux系统部署C++开发环境。

宿主机和Docker

对于Linux系统,选择性很多包括Centos, Redhat, Ubuntu等,大家可以选择在宿主机上直接安装Linux某个版本的系统,也可选择Docker环境部署C++开发环境。
选择Docker的好处如下:
1 可移植性好,不会因为宿主机的差异导致程序移植后无法运行
2 Docker安装的库不会影响宿主机环境,也不会被宿主机环境影响,程序运行环境纯净。
3 共享性好,可以将配置好的容器打包成镜像提交到Docker Hub或者压缩成压缩包供他人使用,提升开发效率。
为了方便给大家演示并提供给大家一个可移植的环境,我选择用Docker配置C++开发环境。

Docker安装

根据不同的Linux系统可以选择不同的方式安装,我的是Ubuntu系统,所以简单列举下我的安装步骤
1 更新系统软件包列表:
执行以下命令以确保您的软件包列表是最新的:

1
sudo apt update

2 安装依赖项以允许使用 HTTPS 通过 APT 获取 Docker:
执行以下命令安装所需的软件包:

1
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

3 添加 Docker GPG 密钥:
执行以下命令以添加 Docker 的官方 GPG 密钥:

1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

4 添加 Docker APT 软件仓库:
执行以下命令以将 Docker APT 软件仓库添加到系统中:

1
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

5 更新软件包列表并安装 Docker:
执行以下命令以更新软件包列表,并安装 Docker CE:

1
2
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

6 验证 Docker 安装是否成功:
执行以下命令以验证 Docker 是否已成功安装:

1
sudo docker run hello-world

如果一切正常,您将看到一个欢迎消息,表示 Docker 已成功安装并正在运行

搭建C++环境

我们基于ubuntu系统的镜像搭建容器,当然你也可以选择其他的系统如centos等,如果小伙伴不喜欢用Docker,可以直接在宿主机操作。

1 启动ubuntu:18.04容器

1
docker run -itd --name cppubuntu ubuntu:18.04

2 容器内更新源,这样能安装最新的软件包

1
apt-get update

3 容器内安装gcc/g++工具包

1
sudo apt install build-essential

4 容器内测试g++版本和命令

1
2
3
4
echo '#include <iostream>' > test.cpp
echo 'int main() { std::cout << __cplusplus << std::endl; return 0; }' >> test.cpp
g++ -std=c++17 test.cpp -o test
./test

输出201703L即证明支持C++17

5 容器内安装wget

1
apt install wget

6 容器内安装boost

6.1 获取源码包

1
wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz

获取速度较慢,我用windows下载好后用filezilla传递到云服务上,再从云服务上执行拷贝操作放入docker

1
docker cp  /home/ubuntu/boost_1_82_0.tar.gz   cppubuntu:/test

6.2 下载boost需要的库

1
apt-get install  python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev 

6.3 解压缩并安装

1
2
3
4
tar zxvf boost_1_82_0.tar.gz
cd ./boost_1_82_0
./bootstrap.sh --prefix=/usr/
./b2 install

6.4 编码测试

1
vim ./boosthello.cpp

写如下代码

1
2
3
4
5
6
7
#include <iostream>
#include <boost/version.hpp>
using namespace std;
int main() {
cout << "Boost 版本" << BOOST_VERSION << endl;
return 0;
}

编译运行

1
g++ -o boosthello ./boosthello.cpp 

执行./boosthello 输出

1
Boost 版本108200

7 容器内安装git

1
apt install git

8 拉取项目代码

1
git clone https://gitee.com/secondtonone1/boostasio-learn.git

9 配置git

1
2
git config --global user.name "secondtonone1"
git config --global user.email "secondtonone1@163.com"

可以验证一下

1
2
git config --global user.name
git config --global user.email

10 下载cmake

1
wget https://github.com/Kitware/CMake/releases/download/v3.27.0/cmake-3.27.0.tar.gz

安装必要的ssl库

1
apt install libssl-dev

解压缩

1
tar -zxvf cmake-3.27.0.tar.gz

设置引导程序

1
./bootstrap

编译

1
make

安装

1
make install

测试安装情况

1
cmake --version

11 编写CMakeLists.txt, 在AsyncServer外层目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cmake_minimum_required(VERSION 3.12)
project(AsyncServer)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)

# 设置 Boost 的路径
set(BOOST_ROOT /usr)

# 查找 Boost 库的组件
find_package(Boost REQUIRED COMPONENTS system thread)

# 添加可执行文件和源文件
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/day06-AsyncServer/*.cpp)

add_executable(AsyncServer ${SOURCES})

# 包含头文件路径(包括其他目录)
target_include_directories(AsyncServer
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/day06-AsyncServer
${CMAKE_CURRENT_SOURCE_DIR}/other_directory
# 添加其他目录路径...
)

# 包含 Boost 头文件路径
target_include_directories(AsyncServer PRIVATE ${Boost_INCLUDE_DIRS})

# 链接 Boost 库
target_link_libraries(AsyncServer PRIVATE ${Boost_LIBRARIES})

新建目录build, 进入build目录,执行cmake ..
生成makefile文件。

在该文件夹执行make 生成可执行文件AsyncServer, 测试可运行。

12 打包容器生成镜像

1
docker commit cppubuntu cppubuntu:1.0

将镜像压缩成tar

1
docker save -o cppubuntu.tar cppubuntu:1.0

传输到百度网盘,大家可以直接使用我的开发环境了。

链接: https://pan.baidu.com/s/1xHP5pSRjFZFiV5qRpgw0KQ?pwd=y468

提取码: y468

总结

本文总结了如何使用Docker搭建Linux C++开发环境,读者也可以根据文档提供的步骤在宿主机直接搭建。