编译链接库

进入boost根目录

运行

bootstrap.bat
.\b2.exe --prefix=D:\platform\boost_1_84_0\BoostInstall

--prefix参数表示存放中间文件的目录

编译完成后会有一下提示:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

D:\platform\boost_1_84_0

The following directory should be added to linker library paths:

D:\platform\boost_1_84_0\stage\lib

这里已经可以直接使用cmake的include_directories 和 link_directories了,但是这不够优雅,显然,要优雅的使用链接库,最好使用find_package命令

cmake配置

稍微翻一下boose的文件,注意到cmake的配置文件在D:\platform\boost_1_84_0\tools\cmake\config

加入find_package的PATHS参数,会有如下报错:

CMake Error at D:/platform/boost_1_84_0/tools/cmake/config/BoostConfig.cmake:67 (find_package):

Could not find a package configuration file provided by "boost_headers"

with any of the following names:

boost_headersConfig.cmake

boost_headers-config.cmake

Add the installation prefix of "boost_headers" to CMAKE_PREFIX_PATH or set

"boost_headers_DIR" to a directory containing one of the above files. If

"boost_headers" provides a separate development package or SDK, be sure it

has been installed.

根据提示,再次翻一下boost文件,注意到提示中的文件在D:\platform\boost_1_84_0\stage\lib\cmake\boost_headers-1.84.0下,根据提示对boost_headers_DIR进行定义,最终得到可行配置:

cmake_minimum_required(VERSION 3.28)
set(ProjectName "asiotest01")
project(${ProjectName})

set(CMAKE_CXX_STANDARD 26)

add_executable(${ProjectName} main.cpp)
set(boost_headers_DIR D:\\platform\\boost_1_84_0\\stage\\lib\\cmake\\boost_headers-1.84.0)
find_package(Boost REQUIRED
        PATHS D:\\platform\\boost_1_84_0\\tools\\cmake\\config
        NO_DEFAULT_PATH
)
if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(${ProjectName} ${Boost_LIBRARIES})
endif ()
set(CLION_INSTALL_PATH "D:/tool/CLion 2023.3")
file(GLOB DLL_FILES "${CLION_INSTALL_PATH}/bin/mingw/bin/*.dll")

foreach(DLL_FILE ${DLL_FILES})
    file(COPY ${DLL_FILE} DESTINATION ${CMAKE_BINARY_DIR})
endforeach()

运行官方示例:

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

参考:https://cmake.org/cmake/help/latest/module/FindBoost.html

https://www.boost.org/doc/libs/1_85_0/more/getting_started/windows.html