编译使用boost库

一、Qt+MingW安装

1、在官网下载boost库

boost_1_70_0.zip

2、将Qt的工具目录(有gcc.exe)设置环境变量。(比如:D:\Qt5.15\Tools\mingw810_64\bin)

3、在命令行进入boost_1_70_1/tools/build/src/engine

4、执行 build.bat gcc,在当前目录将会生成bin.ntx86文件夹,里面包含两个exe文件:b2.exe,bjam.exe

5、将生成的bjam.exe拷贝到解压目录根目录下\boost_1_70

6、进入boost_1_70

7、执行命令 bjam "toolset=gcc" install,等待很久之后会在C盘根目录下生成一个Boost文件夹,我们要使用的头文件与lib就在里边。

8、将7中生成的 Boost 里的 boost文件夹拷贝到qt 的include下面(比如:D:\Qt5.15\5.15.2\mingw81_64\include)

9、将7中生成的 Boost 里的 lib下.a文件拷贝到 qt lib目录下 (比如:D:\Qt5.15\5.15.2\mingw81_64\lib)
https://blog.csdn.net/qq_45662588/article/details/122121487?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3

 

二、测试

①、将C:/Boost复制到新建工程里

 

 

②、配置pro

DEPENDPATH += $$PWD/Boost/lib
INCLUDEPATH += $$PWD/Boost/include/boost-1_78/
LIBS+= $$PWD/Boost/lib/libboost_thread-mgw8-mt-d-x32-1_78.a

注意这是通过配置静态库的全局路径,也可以配置不需要后缀:

LIBS+= -L$$PWD/Boost/lib/ -llibboost_thread-mgw8-mt-d-x32-1_78

相关方法参考:https://www.cnblogs.com/judes/p/15974196.html

③、代码,测试非依赖库的函数

#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <QObject>
#include <functional>
#include <QTimer>
#include <QApplication>
#include <QPushButton>
#include <QDebug>

#include <boost/timer.hpp>
#include <boost/version.hpp>
#include <boost/config.hpp>

using namespace std;

int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    boost::timer time;
    qDebug()<<time.elapsed();
    qDebug()<<BOOST_VERSION<<BOOST_LIB_VERSION<<BOOST_PLATFORM<<BOOST_COMPILER<<BOOST_STDLIB;

    return a.exec();
}

 ⑤、测试线程模块

boost::thread t(&fun);
t.detach();

fun是一个空函数,编译通多则ok

注意需要选对应的Qt版本

 

三、MSVC编译Boost

1、环境

vs2017,编译64位

2、下载boost,1.70

3、打开vs命令行工具

进入boost根目录,执行:

bootstrap.bat

【如果是32位就打开红色框下面那个】

4、生成两个exe:b2和bjam

5、编译静态库

bjam stage --toolset=msvc-14.1 --without-graph --without-graph_parallel --stagedir="C:\\B170" link=static runtime-link=shared runtime-link=static threading=multi debug release

 

 

 

 

 

四、查看需要编译的库

执行:

bjam --show-libraries

 

 

 

pS:

1、参数说明:https://blog.csdn.net/zhangzq86/article/details/90030094

2、库说明:https://zhuanlan.zhihu.com/p/66486828

 

 

3、编译报错:'TIME_UTC' was not declared in this scope

双击错误,打开task_adaptors.hpp,将两个TIME_UTC改为TIME_UTC_,因为TIME_UTC被std拿去用了

4、Mingw只编译线程、日志、单元测试:

bjam.exe --with-thread --with-log --with-test "toolset=gcc" install

 5、Qt中使用asio.hpp报错:Linker error] undefined reference to `WSAStartup@8'

解决:在.pro中添加

LIBS += -lWs2_32

 

posted @ 2019-06-15 15:50  朱小勇  阅读(1460)  评论(0编辑  收藏  举报