CMakeLists.txt 中配置Boost库

本文解决在使用Boost库时出现的未定义引用错误,通过在CMakeLists.txt中正确配置,包括添加定义和链接Boost库,确保编译无误。示例代码展示如何在C++项目中使用Boost线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

未配置正确时报错

‘__static_initialization_and_destruction_0(int, int)’中:
/usr/include/boost/system/error_code.hpp:221:对‘boost::system::generic_category()’未定义的引用
/usr/include/boost/system/error_code.hpp:222:对‘boost::system::generic_category()’未定义的引用
/usr/include/boost/system/error_code.hpp:223:对‘boost::system::system_category()’未定义的引用

CMakeLists.txt中,增加以下配置


add_definitions(-DBOOST_ERROR_CODE_HEADOR_ONLY)# for boost

find_package(Boost REQUIRED COMPONENTS system thread)#for boost

include_directories(${Boost_INCLUDE_DIRS})


# 可执行文件编译时的配置
add_executable(_node_name_
src/_file_name_.cpp)
target_link_libraries(_node_name_ 
   ${PROJECT_NAME}
   ${Boost_LIBRARIES}

   )


 

示例:

src/main.cpp

#include <iostream>
#include <boost/thread/thread.hpp> 
#include <boost/thread/mutex.hpp> 
 
boost::mutex mutex;
 
void print_block(int n, char c)
{

    mutex.lock();
    for (int i = 0; i < n; ++i)
    {
        std::cout << c;
    }
    std::cout << '\n';
    mutex.unlock();
}
 
int main(int argc, char* argv[])
{
    boost::thread thread1(&print_block, 300, '*');
    boost::thread thread2(&print_block, 300, '$');
 
    thread1.join();
    thread2.join();
 
    return 0;
}

CMakeLists.txt配置:

# cmake needs this line
cmake_minimum_required(VERSION 2.8)
 
# Define project name
project(mutex_project)
 
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")
 
## System dependencies are found with CMake's conventions
find_package(Boost REQUIRED COMPONENTS
    thread
)
 
if(NOT Boost_FOUND)
    message("NOT found Boost")
endif()
 
include_directories(${Boost_INCLUDE_DIRS})
# Declare the executable target built from your sources
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})


 

### 配置 CMakeLists.txt 以适配 Windows MSVC 环境 为了在 Windows 上使用 Microsoft Visual Studio Compiler (MSVC) 和 `CMakeLists.txt` 编译 Boost ,可以按照以下方法设置项目结构和构建脚本。 #### 设置 CMakeLists.txt 文件 以下是适用于 Boost 的基本 `CMakeLists.txt` 文件模板: ```cmake # 定义最低版本需求 cmake_minimum_required(VERSION 3.10) # 定义项目名称 project(MyBoostProject) # 查找并引入 Boost find_package(Boost REQUIRED COMPONENTS system filesystem) if(NOT Boost_FOUND) message(FATAL_ERROR "Boost library not found!") endif() # 添加可执行文件目标 add_executable(my_boost_app main.cpp) # 将 Boost 连接到目标 target_include_directories(my_boost_app PRIVATE ${Boost_INCLUDE_DIRS}) target_link_libraries(my_boost_app PRIVATE ${Boost_LIBRARIES}) message(STATUS "Using Boost version: ${Boost_VERSION}") ``` 上述代码片段通过 `find_package` 命令查找本地安装的 Boost ,并将其链接到指定的目标程序中[^1]。如果未找到 Boost,则会抛出错误消息终止构建过程。 #### 使用 CMake 构建项目 完成 `CMakeLists.txt` 后,在命令提示符下运行以下指令来生成适合 MSVC 的解决方案文件(`.sln` 或 `.vcxproj`),具体操作如下所示: ```bash mkdir build cd build cmake -G "Visual Studio 17 2022" .. ``` 此命令中的 `-G` 参数指定了要使用的生成器类型;这里选择了针对最新版 Visual Studio 的工具链支持[^4]。完成后打开生成好的解决方案文件即可继续开发调试工作流程。 另外需要注意的是,当涉及到多目录源码管理时,可以通过辅助函数如 `aux_source_directory()` 来简化对多个子模块的支持[^2]。同时利用 `${CMAKE_CURRENT_SOURCE_DIR}` 变量获取当前正在解析之 CMakelist 路径信息以便动态调整相对路径关系[^3]。 #### 注意事项 确保已正确安装所需版本的 Boost配置好环境变量 PATH 中指向其根目录的位置,这样 CMake 才能顺利定位依赖项。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值