C++: Mac上安装Boost库并使用CLion开发
1.下载安装Boost库
官网下载最新版本1.65.0:http://www.boost.org/users/history/version_1_65_0.html
选择UNIX版本:
下载后解压cd至解压文件夹下
cd /Users/jimmy/Downloads/boost_1_65_0
执行
./booststrap.sh
成功后执行
sudo ./b2 install
几分钟之后编译安装完成
头文件位于 /usr/local/include/boost
库路径位于 /usr/local/lib
2.使用
使用CLion新建一个C++项目
项目名: ttt
CMakeList.txt
CMake查找连接上Boost库,要不然编译会报错,找不到boost
cmake_minimum_required(VERSION 3.8)
project(ttt)
set(CMAKE_CXX_STANDARD 11)
find_package(Boost 1.65.0 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
set(SOURCE_FILES main.cpp)
add_executable(ttt ${SOURCE_FILES})
target_link_libraries(ttt ${Boost_LIBRARIES})
main.cpp
include必须指定boos某个头文件
#include <iostream>
#include <boost/version.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
cout << "Boost版本:" << BOOST_VERSION << endl;
return 0;
}
执行成功
3. 以下是两种编译方法
3.1 自己设定boost头文件和库文件位置
cmake_minimum_required(VERSION 3.6)
#设置项目名称
project(demo)
set(CMAKE_CXX_STANDARD 11)
set(BOOST_ROOT "/usr/local/include/boost")
#添加头文件搜索路径
include_directories(/usr/local/include)
#添加库文件搜索路径
link_directories(/usr/local/lib)
#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)
add_executable(demo ${DIR_SRCS})
#在这里根据名字boost_thread去寻找libboost_thread.a文件
target_link_libraries(demo boost_thread boost_system)
3.2 让Clion自动寻找
cmake_minimum_required(VERSION 2.8.4)
project(BoostTest)
message(STATUS "start running cmake...")
find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(BoostTest main.cpp)
if(Boost_FOUND)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()