install and use boost::thread

本文详细介绍了如何使用Boost库中的Boost.Thread组件。从下载Boost库到编译安装Boost.Thread库的步骤,再到示例代码的实现与编译过程都有详尽说明。

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

I am describing how to use boost::thread libaray below.

step1. download boost from http://www.boost.org/ , e.g. boost_1_52_0.tar.gz

step2. extract the file boost_1_52_0.tar.gz to boost_1_52_0

step3. cd to the boost file, boost_1_52_0, and exectute sh bootstrap.sh --prefix=$HOME/boost --with-libraries=thread

step4.  run ./b2 to generate the libaries of boost thread

step5. run ./b2 install, so .hpp and libs are in $HOME/boost.

torstan: ls $HOME/boost
include lib

---------------------

Here is a sample code in boost_1_52_0/libs/thread/tutorial.


#include <boost/thread/thread.hpp>
#include <iostream>

class factorial
{
public:
factorial(int x, int& res) : x(x), res(res) { }
void operator()() { res = calculate(x); }
int result() const { return res; }

private:
int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }

private:
int x;
int& res;
};

int main()
{
int result;
factorial f(5, result);
boost::thread thrd(f);
thrd.join();
std::cout << "5! = " << result << std::endl;
}

 

----------------

all:
g++ -c factorial.cpp -Ihttp://www.cnblogs.com/include
g++ -o factorial factorial.o http://www.cnblogs.com/lib/libboost_system.a http://www.cnblogs.com/lib/libboost_thread.a -lpthread
clean:
rm factorial *.o

------------------

torstan: ./factorial
5! = 120

 

转载于:https://www.cnblogs.com/Torstan/archive/2013/04/07/3006077.html

<think>好的,用户遇到了找不到Boost库某些模块的问题,具体是filesystem、system、thread、program_options、regex和unit_test_framework这些模块,版本要求1.74.0。我需要帮他解决这个问题。 首先,我得回想一下常见的Boost库安装和配置问题。可能的原因包括Boost没有正确安装、CMake找不到Boost的路径,或者版本不匹配。用户提到的错误信息来自CMake的FindBoost.cmake模块,说明是在CMake配置阶段出的问题。 接下来,我应该分步骤来排查。首先确认Boost是否已经安装,并且版本符合要求。用户需要的是1.74.0,可能系统中安装的是其他版本,或者没有安装。如果使用包管理器安装的,可能需要指定版本,或者从源码编译。 然后是检查安装路径是否正确。有时候Boost可能安装在非标准目录,CMake需要明确指定BOOST_ROOT或Boost_DIR环境变量,或者在CMakeLists.txt中设置这些变量。另外,确保头文件和库文件都存在,并且路径正确。 接下来考虑CMakeLists.txt中的配置。用户可能需要添加find_package(Boost 1.74.0 REQUIRED COMPONENTS ...)语句,并包含所有需要的组件。同时,检查是否正确链接了这些库,使用target_link_libraries。 另外,注意区分静态库和动态库,以及调试版和发布版。CMake在查找时可能需要指定库的类型,比如使用Boost_USE_STATIC_LIB或Boost_USE_STATIC_LIBS变量。 如果用户是从源码编译Boost,可能需要使用b2工具指定安装的组件,并确保编译时生成了所需的库文件。有时候用户可能只安装了头文件,而没有编译库文件,特别是像filesystem、system这些需要编译的组件。 还可能存在系统路径的问题,比如权限问题导致CMake无法读取库文件,或者64位和32位库混淆。需要确认架构是否匹配。 此外,检查CMake的输出信息,查看FindBoost.cmake的日志,了解它搜索的路径和尝试的库名称,这有助于确定问题所在。例如,日志中显示在搜索unit_test_framework的调试版,可能用户只有发布版的库,或者命名方式不符合CMake的预期。 最后,建议用户使用CMake的--debug-find参数来获取更详细的查找过程信息,帮助定位问题。 总结可能的解决步骤:确认安装版本,设置正确的路径变量,检查组件编译情况,调整CMakeLists.txt配置,处理静态/动态库选项,查看详细日志定位问题。</think>以下分步骤说明如何解决Boost库模块缺失问题: ### 1. 确认Boost库安装情况 检查系统中是否已安装Boost 1.74.0: ```bash # Linux查看安装路径 ls /usr/include/boost/version.hpp 2>/dev/null || echo "未找到Boost" # Windows默认路径 C:\local\boost_1_74_0\ ``` 若未安装,需[下载源码](https://www.boost.org/users/history/version_1_74_0.html)[^1] ### 2. 编译必要组件 对于需要编译的模块(如filesystem, system): ```bash ./bootstrap.sh --with-libraries=filesystem,system,thread,program_options,regex,test ./b2 install --prefix=/custom/install/path ``` 注意:`unit_test_framework`对应库名为`test`[^1] ### 3. 配置CMake路径 在CMakeLists.txt中添加: ```cmake set(BOOST_ROOT "/path/to/boost_1_74_0") set(Boost_USE_STATIC_LIBS OFF) # 与安装类型一致 find_package(Boost 1.74.0 REQUIRED COMPONENTS filesystem system thread program_options regex unit_test_framework ) include_directories(${Boost_INCLUDE_DIRS}) target_link_libraries(your_target ${Boost_LIBRARIES}) ``` ### 4. 处理常见错误场景 - **版本不匹配**:检查`Boost_VERSION`输出值 - **库命名规范**:Windows下需匹配编译器版本(如mgw8对应MinGW8) - **调试/发布配置**:使用`Boost_DEBUG=ON`查看搜索过程 ### 5. 验证配置结果 在CMake输出中应出现: ``` Found Boost 1.74.0 at /path/to/boost Found components: filesystem system thread program_options regex unit_test_framework ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值