windows下hiredis的编译,有需要的朋友可以参考下。
果然,高端的程序员真心是鸟都不鸟windows的,redis的客户端找了一圈愣是没有C++的windows版本
我要做个windows上的C++的服务器都没办法和redis交互
github上所有能试的我都试过了,要么是只支持unix,要么是怎么编译都不通过,焦头烂额中
然后我总结了网上无数的教程,附带修复一个个编译错误,总结如下
编译环境,64位windows7 ultimate,VS2013 Ultimate
1.获取redis windows版
2.编译两个lib: hiredis.lib和Win32_Interop.lib
打开从GitHub上clone下来的文件夹,打开里面的msvs文件夹中的RedisServer.sln
从解决方案资源管理器窗口编译hiredis工程和Win32_Interop工程(调试的时候请在debug模式下编译这两个库),此时便会在Debug/Release文件夹下生成这两个工程编译的lib
3.在自己的工程中使用
(1)添加上一步编译的这两个lib到工程中
(2)复制GItHub redis项目文件夹中src/Win32_Interop下所有头文件
(3)以及deps/hiredis下所有头文件(其中fmacros.h用src文件夹下的fmacros.h文件替代)
(4)再复制src/Win32_Interop/win32fixes.c到自己的工程目录,包含到工程文件中
(5)调整各个文件include的路径
(6)示例代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <hiredis.h> #define NO_QFORKIMPL //这一行必须加才能正常使用 #include <Win32_Interop\win32fixes.h> #pragma comment(lib,"hiredis.lib") #pragma comment(lib,"Win32_Interop.lib") int main() { unsigned int j; redisContext *c; redisReply *reply; struct timeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout); if (c->err) { printf("Connection error: %s\n", c->errstr); exit(1); } /* PING server */ reply = (redisReply *)redisCommand(c, "PING"); printf("PING: %s\n", reply->str); freeReplyObject(reply); /* Set a key */ reply = (redisReply *)redisCommand(c, "SET %s %s", "foo", "hello world"); printf("SET: %s\n", reply->str); freeReplyObject(reply); /* Set a key using binary safe API */ reply = (redisReply *)redisCommand(c, "SET %b %b", "bar", 3, "hello", 5); printf("SET (binary API): %s\n", reply->str); freeReplyObject(reply); /* Try a GET and two INCR */ reply = (redisReply *)redisCommand(c, "GET foo"); printf("GET foo: %s\n", reply->str); freeReplyObject(reply); reply = (redisReply *)redisCommand(c, "INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* again ... */ reply = (redisReply *)redisCommand(c, "INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* Create a list of numbers, from 0 to 9 */ reply = (redisReply *)redisCommand(c, "DEL mylist"); freeReplyObject(reply); for (j = 0; j < 10; j++) { char buf[64]; sprintf_s(buf, 64, "%d", j); reply = (redisReply *)redisCommand(c, "LPUSH mylist element-%s", buf); freeReplyObject(reply); } /* Let's check what we have inside the list */ reply = (redisReply *)redisCommand(c, "LRANGE mylist 0 -1"); if (reply->type == REDIS_REPLY_ARRAY) { for (j = 0; j < reply->elements; j++) { printf("%u) %s\n", j, reply->element[j]->str); getchar(); } } freeReplyObject(reply); return 0; }
PS.可能会碰到的编译错误
1.必须定义入口点,请在win32fixes.h之前加上#define NO_QFORKIMPL
2.各种与其他库的使用冲突,请右击项目->属性->配置属性->C/C++->代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)
并且在右击项目->属性->配置属性->连接器->命令行中输入/NODEFAULTLIB:libcmt.lib
3.error C4996,各种unsafe报错啊,请右击项目->属性->配置属性->C/C++->预处理器->预处理器定义->添加“_CRT_SECURE_NO_WARNINGS”(不带引号)