dlib庫學習之一
1、介紹
跨平台 C++ 通用庫 Dlib 發布 ,帶來了一些新特性,包括概率 CKY 解析器,使用批量同步並行計算模型來創建應用的工具,新增兩個聚合算法:中國低語 (Chinese Whispers) 和紐曼的模塊化聚類。
Dlib是一個使用現代C++技術編寫的跨平台的通用庫,遵守Boost Software licence.
主要特點如下:
1.完善的文檔:每個類每個函數都有詳細的文檔,並且提供了大量的示例代碼,如果你發現文檔描述不清晰或者沒有文檔,告訴作者,作者會立刻添加。
2.可移植代碼:代碼符合ISO C++標准,不需要第三方庫支持,支持win32、Linux、Mac OS X、Solaris、HPUX、BSDs 和 POSIX 系統
3.線程支持:提供簡單的可移植的線程API
4.網絡支持:提供簡單的可移植的Socket API和一個簡單的Http服務器
5.圖形用戶界面:提供線程安全的GUI API
6.數值算法:矩陣、大整數、隨機數運算等
7.機器學習算法:
8.圖形模型算法:
9.圖像處理:支持讀寫Windows BMP文件,不同類型色彩轉換
10.數據壓縮和完整性算法:CRC32、Md5、不同形式的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 }