Qt中使用Boost庫


關於boost庫的編譯,請看https://www.cnblogs.com/HackerArt/p/10539516.html

網上可以查到很多介紹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查看輸出


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM