windows vs2013环境配置hiredis

在网上也找了几篇有关windows环境配置hiredis的博客和相关文章,但是我找的10余篇没有一个能测试成功的,后来我自己不断摸索,并测试成功。


1,下载windows版本的reids服务,redis3.0.exe或者redis3.0.msi,网上可下载资源很多


2,下载redis

地址:https://github.com/MSOpenTech/redis

redis-2.8和redis-3.0我没有测试成功,原因是什么,尚不晓得。但是reids-2.6测试成功,可以下载reids-2.6


3,进入reids-2.6/msvs,用vs2013打开RedisServer.sln,解决方案下有7个项目,然后右键,生成,我这里7个项目均生成成功,不过vs2010可能会有几个项不成功,不过不要紧,只要hiredis项目生成成功就可以了,生成成功后,会产生hiredis.lib,在redis-2.6\msvs\Debug中。


4,在vs2013中,新建一个项目,常规项目就可以,然后,右键项目名,属性->配置属性->VC++ 目录->包含目录,将redis-2.6/src和redis-2.6/deps/hiredis的路径加入进去

扫描二维码关注公众号,回复: 1605830 查看本文章


5,右键项目名->属性->配置属性->VC++目录->库目录,将/redis-2.6/msvs/Debug路径加入进去


6,右键项目名->属性->链接器->输入->附加依赖项,添加hiredis.lib和ws2_32.lib


7,最后把redis-2.6/src/下的win32fixes.c放到项目目录下(main.cpp文件在哪就放哪)


8,启动reids服务,运行下面代码,本人已经测成功


#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
extern "C"
{
#include <win32fixes.h>
}
#include <ICRSINT.H>
#include <windows.h>
using namespace std;
void redis()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 1), &wsaData);
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);
}
/* Set a key */
reply = (redisReply *)redisCommand(c, "SET %s %s", "foo", "jingbodasb");
printf("SET: %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);
}


int main(int argc, char *argv[])
{
redis();
system("pause");
}




猜你喜欢

转载自blog.csdn.net/a8530764/article/details/50407894