QT集成Boost库
在Windows平台上,使用Qt集成Boost库,并基于MSVC编译器在CMake文件中加载,可以按照以下步骤进行配置。
- Boost库的编译
如果Boost库未预编译,需要手动编译,解压zip到D:\Library\boost_1_87_0,打开cmd命令行执行以下命令:
> bootstrap.bat
> b2 toolset=msvc address-model=64 --build-type=complete
- address-model=64:指定64位编译(如果是32位系统,改为address-model=32)
- toolset=msvc:指定使用MSVC编译器
- build-type=complete 表示编译所有库
- CMake配置
cmake_minimum_required(VERSION 3.14)
project(QtBoostIntegration)# 设置策略 CMP0144 的行为
if (POLICY CMP0144)cmake_policy(SET CMP0144 NEW)
endif ()# 设置Qt路径(如果未在环境变量中配置)
set(CMAKE_PREFIX_PATH "C:/Qt/6.5.0/msvc2019_64") # 修改为你的Qt安装路径# 查找Qt库
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)# 设置Boost路径
set(BOOST_ROOT "D:/Library/boost_1_87_0") # 修改为你的Boost安装路径
set(Boost_USE_STATIC_LIBS ON) # 表示使用静态链接的 Boost 库
set(Boost_USE_MULTITHREADED ON) # 表示使用多线程的 Boost 库
find_package(Boost 1.87.0 REQUIRED COMPONENTS filesystem system) # 根据需求添加Boost组件
if (Boost_FOUND)include_directories(${Boost_INCLUDE_DIRS})message(STATUS "Boost include directories: ${Boost_INCLUDE_DIRS}")message(STATUS "Boost library directories: ${Boost_LIBRARY_DIRS}")message(STATUS "Boost libraries: ${Boost_LIBRARIES}")
endif ()# 添加可执行文件
add_executable(QtBoostIntegration main.cpp)# 链接Qt库
target_link_libraries(DemoApp1Qt5::CoreQt5::GuiQt5::WidgetsQt5::WebSocketsQt5::Network
)# 链接Boost库
target_link_libraries(DemoApp1Boost::filesystemBoost::system
)
注意
- Boost路径:确保BOOST_ROOT和BOOST_LIBRARYDIR指向正确的Boost安装路径
- Qt路径:如果Qt未在环境变量中配置,确保CMAKE_PREFIX_PATH指向正确的Qt安装路径
- Boost组件:根据项目需求,在find_package(Boost REQUIRED COMPONENTS …)中添加所需的Boost组件