mac 安装与配置boost

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hf19931101/article/details/81068884

1. 下载并解压boost安装包

以boost 1.46.1为例:
https://www.boost.org/users/history/version_1_46_1.html 下载tar包并解压;

2. 安装boost

一部分组件在完成1和2以后就能直接用,因为很多是直接写在hpp的inline函数,但是要利用其它功能,需要build boost库里面的各个组件。

2.1 配置安装路径

#进入到解压后的目录下
cd path/to/boost_1_46_1
#配置安装路径,如果没有prefix参数,则默认安装到/usr/local下面的include和lib目录下
./bootstrap.sh --prefix=path/to/installation/prefix

2.2 执行安装

执行以下命令然后等待即可

./bjam install

2.3 查看安装的库

./bjam --show-libraries

3. 在Clion中使用boost

3.1 配置cmake

在Clion项目中的CMakeList.txt中配置boost相关信息,如下:

#Boost
set(BOOST_ROOT "path/to/installation/prefix")
##设置版本
set(BOOST_MIN_VERSION "1.46.1")
## 链接静态库
set(Boost_USE_STATIC_LIBS ON)
## 动态查找,这种只链接了system,和date_time两个库,可以添加其他的库
find_package(Boost ${BOOST_MIN_VERSION} REQUIRED COMPONENTS system date_time)
if (NOT Boost_FOUND)
    message(FATAL_ERROR "Fatal error:Boost(version >= ${BOOST_MIN_VERSION} required.\n)")
endif ()
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_IIBRARIS: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")

##头文件目录
include_directories(${Boost_INCLUDE_DIRS})
##库文件目录
link_libraries(${Boost_LIBRARIES})

3.2 使用boost

以使用boost中的timer为例:

#include <iostream>
#include <boost/timer.hpp>

int main() {

    boost::timer tm;
    for (int i = 0; i < 100000; ++i) {
        std::cout << "Hello, World!" << std::endl;
    }
    std::cout << tm.elapsed() <<std::endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hf19931101/article/details/81068884