Qt中使用Boost库
关于boost库的编译,请看https://www.cnblogs.com/HackerArt/p/10539516.html
网上可以查到很多介绍qt使用库文件的教程,但是大多都没有注意到,qt中支持设置环境变量这个特性。
这里我拿boost库来举例说明。
qt creator创建项目,设置boost库文件的引入。
将编译生成好的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; }