cmake导入boost使用

通过cmake下载boost release压缩包并在c++项目中使用

cmake_use_boost

1
2
3
4
5
6
7
.
├── CMakeLists.txt
├── build_and_run.sh
├── main.cc
├── output.txt
└── thirdparty
    └── boost

CMakeLists.txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cmake_minimum_required(VERSION 3.28)
project(cmake_use_boost)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 设置三方库存放目录为 项目根目录/thirdparty
set(THIRDPARTY_DIR ${CMAKE_SOURCE_DIR}/thirdparty)
message("thirdparty_dir: " ${THIRDPARTY_DIR})

include(FetchContent)

# import boost
# 设置需要使用的 boost 库
set(BOOST_INCLUDE_LIBRARIES bimap)
set(BOOST_ENABLE_CMAKE ON)
FetchContent_Declare(
    boost
    # boost release 下载地址
    URL https://github.com/boostorg/boost/releases/download/boost-1.86.0/boost-1.86.0-cmake.tar.gz
    DOWNLOAD_EXTRACT_TIMESTAMP ON
    # 下载后解压到本地的目录
    SOURCE_DIR ${THIRDPARTY_DIR}/boost
    EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(boost)

add_executable(main main.cc)
# 链接 boost 库
target_link_libraries(main boost_bimap)

main.cc

1
2
3
4
5
6
7
8
#include "boost/bimap.hpp"
int main() {
    using bm_type = boost::bimap<int, std::string>
    using bm_value_type = bm_type::value_type;
    bm_type bm;
    bm.insert(bm_value_type(1, "one"));
    return 0;
}
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计