Qt中使用Boost库

关于boost库的编译,请看https://my.oschina.net/yeahlife/blog/3086364

网上可以查到很多介绍qt使用库文件的教程,但是大多都没有注意到,qt中支持设置环境变量这个特性。

这里我拿boost库来举例说明。

qt creator创建项目,设置boost库文件的引入。

image

将编译生成好的lib目录,添加到LIB或者Path,

boost库头文件不要添加到INCLUDE中,加到这里qt会提示不识别,

需要将boost库头文件添加到qt的pro配置文件中。

提示:boost源目录下的boost目录中的文件 就可以作为include头文件,不需要额外生成,

# Boost 1_69
# boost头文件目录
INCLUDEPATH += D:\boost\include

qt项目中添加测试代码

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <Windows.h>
#include <qdebug.h>
using namespace std;

void TestBoost()
{
    using boost::lexical_cast;
    int a = lexical_cast<int>("123");
    double b = lexical_cast<double>("123.0123456789");
    string s0 = lexical_cast<string>(a);
    string s1 = lexical_cast<string>(b);
    //cout << "number: " << a << "  " << b << endl;
    //cout << "string: " << s0 << "  " << s1 << endl;
    //OutputDebugStringA(a);
    qDebug() << a << b << endl;
    qDebug() << s0.c_str() << s1.c_str() << endl;
    //OutputDebugStringA(s1);
    int c = 0;
    try {
        c = lexical_cast<int>("abcd");
    }
    catch (boost::bad_lexical_cast& e) {
        //cout << e.what() << endl;
    }
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    TestBoost();
}

MainWindow::~MainWindow()
{
    delete ui;
}

清理,重新构建(如果没效果执行qmake)打开debugView查看输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值