从官网下载并解压到适当位置
Boost网站
在解压后的目录中找到
bootstrap.bat点击运行,并等待结束
这时会出现b2.exe文件,点击运行,耐心等待结束
(安装后产生的文件会占据大约4.3GB,请确保磁盘空间充足)
文档地址
只寻找头文件
find_package(Boost 1.36.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo foo.cc)
endif()
寻找库文件并引入到目标文件中
find_package(Boost 1.56 REQUIRED COMPONENTS date_time filesystem iostreams) add_executable(foo foo.cc) target_link_libraries(foo Boost::date_time Boost::filesystem Boost::iostreams)
下面以引入Python模块作为示例,读者可仿照此例引入自己需要的模块。
寻找库“python3.6”并引入
find_package(Boost 1.67 REQUIRED COMPONENTS python36 numpy36) add_executable(foo foo.cc) target_link_libraries(foo Boost::python36 Boost::numpy36)
寻找头文件和静态库文件并引入
set(Boost_USE_STATIC_LIBS ON) # only find static libs
set(Boost_USE_DEBUG_LIBS OFF) # ignore debug libs and
set(Boost_USE_RELEASE_LIBS ON) # only find release libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.66.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()
运行测试
项目配置
# CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(test_0_1)
set(CMAKE_CXX_STANDARD 17)
set(Boost_INCLUDE_DIRS "D:/ProgramData/boost_1_78_0/" )
set(Boost_LIBRARIES "D:/ProgramData/boost_1_78_0/stage/lib")
include_directories(${Boost_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp)
主程序
//main.cpp #include//包含 Boost 头文件 #include //包含 Boost 头文件 #include using namespace std; int main() { cout << BOOST_VERSION << endl; //Boost 版本号 cout << BOOST_LIB_VERSION << endl; //Boost 版本号 cout << BOOST_PLATFORM << endl; //操作系统 cout << BOOST_COMPILER << endl; //编译器 cout << BOOST_STDLIB << endl; //标准库 return 0; }
输出:
107800 1_78 Win32 GNU C++ version 11.2.0 GNU libstdc++ version 20210728



