版本:Windows下安装的是1.81.0,Ubantu下安装的是1.82.0
开发环境搭建
Windows搭建Boost开发环境
- 下载好MinGW并配置好c++编译器,c编译器等的环境变量:从以下输出可见我此次编译boost库使用的是GNU编译器套件
C:\Users\wzh>gcc --version
gcc (x86_64-posix-seh-rev3, Built by MinGW-W64 project) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\Users\wzh>g++ --version
g++ (x86_64-posix-seh-rev3, Built by MinGW-W64 project) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\Users\wzh>mingw32-make --version
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
- 在命令行窗口下依次执行:
# 指定工具集为gcc(MinGW GCC),当然也可以指定其他的比如说vc142,对应工具集为Visual C++ 14.2 (VS 2019)
D:\Boost\boost_1_81_0>bootstrap.bat gcc
D:\Boost\boost_1_81_0>b2
....
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
D:\Boost\boost_1_81_0
The following directory should be added to linker library paths:
D:\Boost\boost_1_81_0\stage\lib
- 编译成功以后会在boost_1_81_0\stage\lib目录下生成库文件,包含release和debug版本的。
- 测试程序:main.cpp
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/version.hpp>
int main() {
// 1. 测试 Boost 版本
std::cout << "Boost 版本: "
<< BOOST_VERSION / 100000 << "."
<< BOOST_VERSION / 100 % 1000 << "."
<< BOOST_VERSION % 100
<< std::endl;
// 2. 测试 filesystem:获取当前路径
boost::filesystem::path current_path = boost::filesystem::current_path();
std::cout << "当前路径: " << current_path.string() << std::endl;
std::cout << "Boost 测试通过!" << std::endl;
return 0;
}
- CMake配置如下所示:
cmake_minimum_required(VERSION 3.10)
project(BoostTest)
set(CMAKE_CXX_STANDARD 11)
# 直接手动指定路径
set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/boost1.81.0)
set(Boost_INCLUDE_DIR ${BOOST_ROOT}/include)
set(Boost_LIBRARY_DIR ${BOOST_ROOT}/lib)
# 根据构建类型设置库文件名后缀
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(BOOST_SUFFIX "-d")
else()
set(BOOST_SUFFIX "")
endif()
# 指定项目使用到的库文件
set(BOOST_LIBRARIES
${Boost_LIBRARY_DIR}/libboost_filesystem-mgw12-mt${BOOST_SUFFIX}-x64-1_81.a
${Boost_LIBRARY_DIR}/libboost_system-mgw12-mt${BOOST_SUFFIX}-x64-1_81.a
)
add_executable(boost_test main.cpp)
target_include_directories(boost_test PRIVATE ${Boost_INCLUDE_DIR})
target_link_libraries(boost_test ${BOOST_LIBRARIES})
- 编译运行:
# 在项目根目录下创建build目录
mkdir build
cd build
# 指定生成器,指定构建Debug版本的程序,生成Makefile
cmake -G "MinGW Makefiles" -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -DCMAKE_BUILD_TYPE=Debug ..
# 编译
mingw32-make -j4
# 运行
C:\Users\wzh\Desktop\BoostTest\build>boost_test.exe
Boost 版本: 1.81.0
当前路径: C:\Users\wzh\Desktop\BoostTest\build
Boost 测试通过!
Linux下搭建Boost开发环境
- 安装Docker
# 更新软件包索引
sudo apt update
# apt-transport-https:允许 apt 使用 HTTPS。
# ca-certificates:允许系统识别并验证 SSL 证书
# curl:一个通过 URL 传输数据的命令行工具
# software-properties-common:提供了管理软件源的脚本
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# 下载并添加 Docker 官方GPG密钥,用于后续验证Docker软件包的签名是否真实、未被篡改
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 将 Docker 的官方软件源地址添加到你的系统中
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
# 更新软件包索引,然后正式安装 Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# 验证docker是否安装成功,可以查看docker的运行状态
sudo systemctl status docker
- 更改镜像源
sudo vim /etc/docker/daemon.json
# 该文件添加如下内容
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.1ms.run",
"https://hub.1panel.dev",
"https://docker.1panel.live"
]
}
# 重启docker服务
sudo systemctl daemon-reload
sudo systemctl restart docker
- 创建一个名为cpp-dev的Docker容器,基于Ubuntu22.04镜像,并在后台运行,同时保持容器可交互。
docker run -itd --name cpp-dev ubuntu:22.04
- 安装boost,安装完成后会在/usr/local/boost_1_82_0/include目录下存在头文件,在/usr/local/boost_1_82_0/lib目录下存在库文件
# 进入容器
sudo docker exec -it cpp-dev /bin/bash
apt-get update
apt install build-essential
apt install wget
wget https://archives.boost.io/release/1.82.0/source/boost_1_82_0.tar.gz
apt-get install python2-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev cmake vim
tar zxvf boost_1_82_0.tar.gz
cd ./boost_1_82_0
# prefix指定安装目录
./bootstrap.sh --prefix=/usr/local/boost_1_82_0
./b2 install
- 测试:注意Ubuntu 22.04 系统默认安装了 Boost 1.74.0,请确保程序中使用的是你安装的版本呢。
- CMake配置如下:
cmake_minimum_required(VERSION 3.12) project(boost_test) # 设置 C++ 标准 set(CMAKE_CXX_STANDARD 17) # 设置 Boost 的路径 set(BOOST_ROOT /usr/local/boost_1_82_0) # 查找 Boost 库的组件 find_package(Boost REQUIRED COMPONENTS system filesystem) # 调试输出 message(STATUS "Boost_FOUND = ${Boost_FOUND}") message(STATUS "Boost_VERSION = ${Boost_VERSION}") message(STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}") message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}") message(STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}") # 查看查找时使用的路径 message(STATUS "BOOST_ROOT = ${BOOST_ROOT}") message(STATUS "CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}") # 源文件列表 file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) add_executable(boost_test ${SOURCES}) # 头文件路径 target_include_directories(boost_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ ${Boost_INCLUDE_DIRS} ) # 链接 Boost 库 target_link_libraries(boost_test PRIVATE ${Boost_LIBRARIES})- 源程序同上main.cpp
- 编译运行:
cmake .. -DCMAKE_BUILD_TYPE=Debug make -j4 ./boost_test - 开发环境打包:统一团队之间的开发环境
# 打包容器,生成镜像
docker commit cpp-dev cpp-dev:1.0
# 将镜像压缩
docker save -o cpp-dev.tar cpp-dev:1.0
浙公网安备 33010602011771号