Header-only(?)
Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.
时间 2025-05
版本 boost_1_88_0
测试环境 Ubuntu 24.04 (WSL 2) 非root 当然gcc/g++肯定是必需的
使用Header-only的库
下载代码解压后,在Include Path中添加:/some_path/boost_1_88_0/;或者使用:-I/path/to/boost_1_88_0
使用需要编译的库
./bootstrap.sh # 配置构建工具
./b2 # 全部编译
# 可以附加参数(--with-filesystem --with-system等)指定需要编译的模块
# 编译后的提示(按照指示配置即可)
...
The following directory should be added to compiler include paths:
/home/zh-ge/boost_1_88_0
The following directory should be added to linker library paths:
/home/zh-ge/boost_1_88_0/stage/lib
# 查看编译结果(静态库和动态库都是有的)
zh-ge@DESKTOP-3UMDA4O:~/boost_1_88_0/stage/lib$ ls -lh
total 43M
drwxr-xr-x 54 zh-ge zh-ge 4.0K Jun 5 17:51 cmake
-rw-r--r-- 1 zh-ge zh-ge 15K Jun 5 17:51 libboost_atomic.a
lrwxrwxrwx 1 zh-ge zh-ge 25 Jun 5 17:50 libboost_atomic.so -> libboost_atomic.so.1.88.0
-rwxr-xr-x 1 zh-ge zh-ge 22K Jun 5 17:50 libboost_atomic.so.1.88.0
-rw-r--r-- 1 zh-ge zh-ge 270K Jun 5 17:52 libboost_charconv.a
lrwxrwxrwx 1 zh-ge zh-ge 27 Jun 5 17:51 libboost_charconv.so -> libboost_charconv.so.1.88.0
-rwxr-xr-x 1 zh-ge zh-ge 195K Jun 5 17:51 libboost_charconv.so.1.88.0
-rw-r--r-- 1 zh-ge zh-ge 153K Jun 5 17:51 libboost_chrono.a
lrwxrwxrwx 1 zh-ge zh-ge 25 Jun 5 17:50 libboost_chrono.so -> libboost_chrono.so.1.88.0
-rwxr-xr-x 1 zh-ge zh-ge 49K Jun 5 17:50 libboost_chrono.so.1.88.0
...
# 没有问题,可以安装(只是不好控制版本) sudo apt update sudo apt install libboost-all-dev # 安装后的结果 zh-ge@DESKTOP-3UMDA4O:~$ apt list | grep 'boost' WARNING: apt does not have a stable CLI interface. Use with caution in scripts. dm-writeboost-dkms/noble-updates 2.2.16-0.1ubuntu2.1 all ibus-typing-booster/noble 2.25.4-1 all libboost-all-dev/noble,now 1.83.0.1ubuntu2 amd64 [installed] libboost-atomic-dev/noble,now 1.83.0.1ubuntu2 amd64 [installed,automatic] libboost-atomic1.74-dev/noble 1.74.0+ds1-23.1ubuntu3 amd64 libboost-atomic1.74.0/noble 1.74.0+ds1-23.1ubuntu3 amd64 libboost-atomic1.83-dev/noble-updates,now 1.83.0-2.1ubuntu3.1 amd64 [installed,automatic] libboost-atomic1.83.0/noble-updates,now 1.83.0-2.1ubuntu3.1 amd64 [installed,automatic] libboost-chrono-dev/noble,now 1.83.0.1ubuntu2 amd64 [installed,automatic] libboost-chrono1.74-dev/noble 1.74.0+ds1-23.1ubuntu3 amd64 libboost-chrono1.74.0t64/noble 1.74.0+ds1-23.1ubuntu3 amd64 ...
Header-only 的部分肯定是直接编译到最终的程序中去了
g++ main.cpp -o my_program \
-I~/boost_1_88_0 \
~/boost_1_88_0/stage/lib/libboost_system.a \
~/boost_1_88_0/stage/lib/libboost_filesystem.a
如果不设置环境变量的话,运行时可能找不到 .so(动态库)
g++ main.cpp -o my_program \
-I~/boost_1_88_0 \
-L~/boost_1_88_0/stage/lib \
-lboost_system -lboost_filesystem
# 添加环境变量
export LD_LIBRARY_PATH=~/boost_1_88_0/stage/lib:$LD_LIBRARY_PATH