dlib庫學習之一html
一、介紹ios
跨平臺 C++ 通用庫 Dlib 發佈 ,帶來了一些新特性,包括機率 CKY 解析器,使用批量同步並行計算模型來建立應用的工具,新增兩個聚合算法:中國低語 (Chinese Whispers) 和紐曼的模塊化聚類。算法
Dlib是一個使用現代C++技術編寫的跨平臺的通用庫,遵照Boost Software licence.安全
主要特色以下:服務器
1.完善的文檔:每一個類每一個函數都有詳細的文檔,而且提供了大量的示例代碼,若是你發現文檔描述不清晰或者沒有文檔,告訴做者,做者會馬上添加。網絡
2.可移植代碼:代碼符合ISO C++標準,不須要第三方庫支持,支持win3二、Linux、Mac OS X、Solaris、HPUX、BSDs 和 POSIX 系統app
3.線程支持:提供簡單的可移植的線程API框架
4.網絡支持:提供簡單的可移植的Socket API和一個簡單的Http服務器機器學習
5.圖形用戶界面:提供線程安全的GUI APIsocket
6.數值算法:矩陣、大整數、隨機數運算等
7.機器學習算法:
8.圖形模型算法:
9.圖像處理:支持讀寫Windows BMP文件,不一樣類型色彩轉換
10.數據壓縮和完整性算法:CRC3二、Md五、不一樣形式的PPM算法
11.測試:線程安全的日誌類和模塊化的單元測試框架以及各類測試assert支持
12.通常工具:XML解析、內存管理、類型安全的big/little endian轉換、序列化支持和容器類
2.安裝使用
這個和boost使用方法有點像,但小得多,只要下載源碼包就可使用,不須要其餘的三方庫,幫助文檔說了只要添加頭文件引用就能夠,若是報連接錯誤須要把all/source.cpp包含在項目中,這個cpp也只是包含一些頭文件,假如不須要GUI功能就能夠在這個定義宏
DLIB_NO_GUI_SUPPORT 這樣能夠減少執行文件大小 ,其餘的同樣
How to compile
To use this library all you have to do is extract it somewhere, make sure the folder containing the dlib folder is in your include path, and finally add dlib/all/source.cpp to your project. It is worth noting that most of dlib is "header-only" which means that, in many cases, you don't actually have to build dlib/all/source.cpp into your application. So if you don't get linker errors when you exclude dlib/all/source.cpp from your project then you don't need it.
3.小試牛刀
這個例子介紹如何使用dlib ,定時器和client、server pipe信息
將dlib文件夾包含在項目的LINCLUDEPATH中
這裏用到了socket和線程因此須要包含 dlib/all/source.cpp
我是用mingw 編譯的因此須要指定要連接的系統庫,這樣編譯就不會報錯了
SOURCES += main.cpp \ D:/Libs/dlib-18.10/dlib/all/source.cpp LIBS += -lwsock32 -lws2_32 -limm32 -luser32 -lgdi32 -lcomctl32 INCLUDEPATH += D:/Libs/dlib-18.10
client代碼
1 #include <iostream> 2 3 #include <dlib/bridge.h> 4 #include <dlib/type_safe_union.h> 5 #include <dlib/timer.h> 6 7 using namespace std; 8 using namespace dlib; 9 10 //管道 11 dlib::pipe<string> out(4),in(4); 12 13 14 //定時器類 15 class timer_task 16 { 17 public: 18 //定時執行的函數 19 void timer_send() 20 { 21 string msg("this client msg"); 22 out.enqueue(msg); 23 24 std::string re; 25 26 in.dequeue(re); 27 cout<<"client receive:"<<re<<endl; 28 29 } 30 31 }; 32 33 34 35 36 int main() 37 { 38 39 //這裏應該是一個連接tcp server ,由於我開兩個client只有一個能收到信息,關閉一個後另外一個就能收到 40 bridge b1(connect_to_ip_and_port("127.0.0.1", 12345), transmit(out),receive(in)); 41 42 43 44 timer_task task; 45 46 //這個timer應該不和main在一個線程,應爲若是不加下面的 dlib::sleep 程序會直接退出 47 timer<timer_task> t(task,&timer_task::timer_send); 48 49 t.set_delay_time(1000); 50 51 t.start(); 52 53 54 dlib::sleep(10000000); 55 56 return 0; 57 }
server
1 #include <iostream> 2 3 #include <dlib/bridge.h> 4 #include <dlib/type_safe_union.h> 5 #include <dlib/timer.h> 6 7 using namespace std; 8 using namespace dlib; 9 10 dlib::pipe<string> in(4),out(4); 11 12 13 14 class timer_task 15 { 16 public: 17 void timer_send() 18 { 19 string msg; 20 in.dequeue(msg); 21 cout<<"service receive:"<<msg<<endl; 22 23 24 std::string value = "this is server send"; 25 out.enqueue(value); 26 27 } 28 29 }; 30 31 32 33 int main() 34 { 35 cout << "Hello World!" << endl; 36 37 bridge b1(listen_on_port(12345),transmit(out), receive(in)); 38 39 timer_task task; 40 41 timer<timer_task> t(task,&timer_task::timer_send); 42 43 t.set_delay_time(1000); 44 45 t.start(); 46 dlib::sleep(10000000); 47 48 return 0; 49 }