假設"D:\boost_1_77_0"是boost庫所在路徑。
1,在QT項目的.pro文件中修改如下兩項:
在INCLUDEPATH項中加入D:\boost_1_77_0
在LIBS項中加入-LD:\boost_1_77_0
注意:LIBS項中的boost路徑前面需要加上-L,並且和boost路徑之間沒有空格。
2,在QT項目中引入所需的boost頭文件:
此處以boost::optional為例。
如下所示,第2行和第3行代碼引入了optional相關頭文件。
1 #include <QCoreApplication>
2 #include <boost\optional.hpp>
3 #include <boost\optional\optional_io.hpp>
4 #include <iostream>
5
6 int main(int argc, char *argv[]) 7 { 8 QCoreApplication a(argc, argv); 9
10 boost::optional<int> nullable_int1 = {1}; 11 boost::optional<int> nullable_int2 = boost::none; 12
13 std::cout << nullable_int1 << std::endl; 14
15 if(nullable_int2 == boost::none) 16 { 17 std::cout << "null" << std::endl; 18 } 19
20 return a.exec(); 21 }