参考文章如下:(俺还是很有版权意识滴,尊重原作者的劳动成果)
http://www.cnblogs.com/freegodly/p/4259040.html?utm\_source=tuicool&utm\_medium=referral
关于Dlib库的编译,网上都是依赖CMAKE,其实并不需要,用gcc或者mingw就可以的。
具体实践如下
1.下载最新Dlib库:
dlib-19.4.zip(http://dlib.net/files/dlib-19.4.zip)
2.解压缩(我的是E:\Dlib\dlib-19.4),你们随意
3.用Qt库新建工程
*.pro文件需要追加(重点)
1).CONFIG += c++11(dlib是用C++11)
2).SOURCES += E:/Dlib/dlib-19.4/dlib/all/source.cpp(为防止遗漏的头文件,就用这个)
3).LIBS += -lwsock32 -lws2_32 -limm32 -luser32 -lgdi32 -lcomctl32 -lwinmm
(这个就是依赖系统的库)
4).INCLUDEPATH += E:/Dlib/dlib-19.4(指定头文件的路径)
注,不知道是否为版本差异,参考那个文章没有追加(-lwinmm链接),会报下面这个错误
E:\Dlib\dlib-19.4\dlib\misc_api\misc_api_kernel_1.cpp:98:
error: undefined reference to `_imp__timeGetTime@0'
追踪代码后发现依赖window的DWORD WINAPI timeGetTime系统函数,百度一下就知道这个库依赖Winmm,
代码为参考文章的:
客户端:
#include <iostream>
#include <dlib/bridge.h>
#include <dlib/type_safe_union.h>
#include <dlib/timer.h>
using namespace std;
using namespace dlib;
//管道
dlib::pipe<string> out(4),in(4);
//定时类
class timer_task
{
public:
void timer_send()
{
string msg("this client msg");
out.enqueue(msg);
string re;
in.dequeue(re);
cout << "client receive :" << re << endl;
}
};
int main()
{
bridge b1(connect_to_ip_and_port("127.0.0.1", 12345), \
transmit(out), receive(in));
timer_task task;
timer<timer_task> t(task, &timer_task::timer_send);
t.set_delay_time(1000);
t.start();
dlib::sleep(10000000);
cout << "Hello World!" << endl;
return 0;
}
服务器端:
#include <iostream>
#include<dlib/bridge.h>
#include<dlib/type_safe_union.h>
#include<dlib/timer.h>
using namespace std;
using namespace dlib;
dlib::pipe<string> out(4),in(4);
//定时类
class timer_task
{
public:
void timer_send()
{
string msg;
in.dequeue(msg);
cout << "server receive :" << msg << endl;
string value = "this is server send";
out.enqueue(value);
}
};
int main()
{
cout << "Hello World!" << endl;
bridge b1(listen_on_port(12345), transmit(out), receive(in));
timer_task task;
timer<timer_task> t(task,&timer_task::timer_send);
t.set_delay_time(1000);
t.start();
dlib::sleep(10000000);
return 0;
}
运行结果如下: