0%

boost::format使用

1. boost安装

1.1. yum安装

进入root帐户,运行以下三行命令即可

1
2
3
yum install boost
yum install boost-devel
yum install boost-doc

安装完毕后,可以在usr/include里面找到头文件,cmake配置好后一直没找到库文件,Boost_LIBRARIES的值一直为空,值为空的原因是我只是find_package(Boost),没有指定组件,指定了还报错的话,个人猜测可能是上面几行命令还不够,因为yum list | grep boost后除了以上三个安装包外,还有很多库文件的包,如boost-filesystem.i686,虽然可以通过yum install boost*暴力安装,但也不清楚有没有缺东西,所以采用压缩包安装的方式。如果只是用一些包的话,也可以用yum把需要的包装上。

1.2. 压缩包安装

1.2.1. 官网下载boost

下载boost,下载完成后解压,并进入解压缩后的目录

1
2
tar --bzip2 -xf boost_1_57_0.tar.bz2
cd boost_1_57_0

建议下载tar.gz文件,bz2文件可能构建Jamfile,解压缩时注意解压完全

1.2.2. 编译安装boost

打开目录下的index.html网页,进入Getting_started,在右下角点击Getting started on Unix variants(e.g.Linux, MacOS) ,然后按照官方给的帮助进行安装。简单编译安装如下:

1
2
./bootstrap.sh --prefix=path/to/installation/prefix
./b2 install

如果安装后想马上使用boost库进行编译,还需要执行一下这个命令:

1
ldconfig

更新一下系统的动态链接库

2. Linux下cmake编译时链接boost库

2.1. find_package

find_package是CMake打包配置文件的工具。Find Boost包括目录和库
通过以下形式调用find_package来使用此模块:

1
2
3
4
5
find_package(Boost
[version] [EXACT] # Minimum or EXACT version e.g. 1.36.0
[REQUIRED] # Fail with error if Boost is not found
[COMPONENTS <libs>...] # Boost libraries by their canonical name
) # e.g. "date_time" for "libboost_date_time"

find_package必须小写,以下变量名注意大小写
在上述命令成功运行完后,可以得到保存路径等信息的变量,如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Boost_FOUND            - True if headers and requested libraries were found
Boost_INCLUDE_DIRS - Boost include directories
Boost_LIBRARY_DIRS - Link directories for Boost libraries
Boost_LIBRARIES - Boost component libraries to be linked
Boost_<C>_FOUND - True if component <C> was found (<C> is upper-case)
Boost_<C>_LIBRARY - Libraries to link for component <C> (may include
target_link_libraries debug/optimized keywords)
Boost_VERSION - BOOST_VERSION value from boost/version.hpp
Boost_LIB_VERSION - Version string appended to library filenames
Boost_MAJOR_VERSION - Boost major version number (X in X.y.z)
Boost_MINOR_VERSION - Boost minor version number (Y in x.Y.z)
Boost_SUBMINOR_VERSION - Boost subminor version number (Z in x.y.Z)
Boost_LIB_DIAGNOSTIC_DEFINITIONS (Windows)
- Pass to add_definitions() to have diagnostic
information about Boost's automatic linking
displayed during compilation

在搜索package前,可以设置变量辅助搜索

1
2
3
4
5
6
7
8
9
BOOST_ROOT             - Preferred installation prefix
(or BOOSTROOT)
BOOST_INCLUDEDIR - Preferred include directory e.g. <prefix>/include
BOOST_LIBRARYDIR - Preferred library directory e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS - Set to ON to disable searching in locations not
specified by these hint variables. Default is OFF.
Boost_ADDITIONAL_VERSIONS
- List of Boost versions not known to this module
(Boost install locations may contain the version)

将搜索结果永久保存在CMake缓存条目中:

1
2
3
4
Boost_INCLUDE_DIR         - Directory containing Boost headers
Boost_LIBRARY_DIR - Directory containing Boost libraries
Boost_<C>_LIBRARY_DEBUG - Component <C> library debug variant
Boost_<C>_LIBRARY_RELEASE - Component <C> library release variant

仅查找Boost头文件的示例:

1
2
3
4
5
find_package(Boost 1.36.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo foo.cc)
endif()

查找Boost头文件和一些静态库的示例:

1
2
3
4
5
6
7
8
9
set(Boost_USE_STATIC_LIBS        ON) # only find static libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.36.0 COMPONENTS date_time filesystem system ...)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo foo.cc)
target_link_libraries(foo ${Boost_LIBRARIES})
endif()

示例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cmake_minimum_required(VERSION 2.8)
PROJECT(WritePts)
SET(WritePtsSource
./source/WritePts.cpp
)

set(BOOST_ROOT /usr/local)
find_package(Boost 1.57.0 COMPONENTS regex filesystem)
#find_package(Boost)

if(Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")

ADD_EXECUTABLE(WritePts ${WritePtsSource})
TARGET_LINK_LIBRARIES(WritePts ${Boost_LIBRARIES})
endif()

注意文件中各个变量的大小写,如果这样设置后,还存在报错,第一步先核实一下各个变量名,如果没有问题,再根据报错信息确定可能出现的原因

示例2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# CMakeLists.txt
project(tutorial-0)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 14)

set(BOOST_ROOT /usr/local/install/boost_1_61_0)

find_package(Boost COMPONENTS regex system REQUIRED)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})

MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")

add_executable(foo foo.cpp)
target_link_libraries (foo ${Boost_LIBRARIES})
endif()

通过设置BOOST_ROOT来设置首选的搜索路径
通过MESSAGE函数把查找的结果都打印了出来

1
2
3
-- Boost_INCLUDE_DIRS = /usr/local/install/boost_1_61_0/include.
-- Boost_LIBRARIES = /usr/local/install/boost_1_61_0/lib/libboost_regex.so;/usr/local/install/boost_1_61_0/lib/libboost_system.so.
-- Boost_LIB_VERSION = 1_61.

编译过程需要用到的头文件搜索路径保存在变量Boost_INCLUDE_DIRS中,所需要link的库文件路径保存在变量Boost_LIBRARIES中

参考链接FindBoost

3. boost::format使用

安装好boost,配置好cmake之后,引入头文件format.cpp

1
#include <boost/format.hpp>