Redis,GetStart on windows

原创
2013/06/04 11:42
阅读数 1.5K

        redis 一个可扩展的高性能的nosql数据库,越来越多的产品在使用它,redis是目前只能在linux下编译,这么好的东西肯定会有人移植到window平台的,点击msopentech移植的版本.

这里先说一下测试环境

系统:window xp sp3

编译器:VC6

由于Redis是独立的进程,不需要嵌入到程序,所以不涉及到兼容性,这里直接使用Redis-server的win32移植版.

但是客户端是需要嵌入到其他的程序的,所以VC6能编译,那基本上msvc所有版本的都可以使用了.

客户端代码使用hiredis,编译的时候需要做一些修改.


  1. 将long long 修改成__int64
  2. win32fixes.c 496行由 
    d = _strtod_l(nptr, &leptr, clocale);
     改成 
    d = strtod(nptr, &leptr);
  3. 删除win32fixes.c 495,494,490三行
  4. win32fixes.c 115行的intptr_t 改成 int


新建一个win32 console工程,然后将 hiredis 目录下面的代码都放进去,新建一个test.c将一下代码复制进去,将工程由Debug SingleThread 改成Debug MutliThread(Project->Settings->C/C++ Code Generation).
#include "win32fixes.h"
#include "fmacros.h"
#include "hiredis.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
#pragma comment(lib,"ws2_32.lib")


int main()
{
	unsigned int j;
    redisContext *c;
    redisReply *reply;

	WSADATA wsa;
    struct timeval t = { 1, 500000 }; // 1.5 seconds

	WSAStartup(MAKEWORD(2,2),&wsa);

    c = redisConnectWithTimeout("127.0.0.1",6379, t);
    if (c->err) {
        printf("Connection error: %s\n", c->errstr);
        exit(1);
    }
	
    /* PING server */
    reply = redisCommand(c,"PING");
    printf("PING: %s\n", reply->str);
    freeReplyObject(reply);
	
    /* Set a key */
    reply = redisCommand(c,"SET %s %s", "foo", "hello world");
    printf("SET: %s\n", reply->str);
    freeReplyObject(reply);
	
    /* Set a key using binary safe API */
    reply = 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 = redisCommand(c,"GET foo");
    printf("GET foo: %s\n", reply->str);
    freeReplyObject(reply);
	
    reply = redisCommand(c,"INCR counter");
    printf("INCR counter: %lld\n", reply->integer);
    freeReplyObject(reply);
    /* again ... */
    reply = redisCommand(c,"INCR counter");
    printf("INCR counter: %lld\n", reply->integer);
    freeReplyObject(reply);
	
    /* Create a list of numbers, from 0 to 9 */
    reply = redisCommand(c,"DEL mylist");
    freeReplyObject(reply);
    for (j = 0; j < 10; j++) {
        char buf[64];
		
        snprintf(buf,64,"%d",j);
        reply = redisCommand(c,"LPUSH mylist element-%s", buf);
        freeReplyObject(reply);
    }
	
    /* Let's check what we have inside the list */
    reply = 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);
        }
    }
    freeReplyObject(reply);

	WSACleanup();
	return 0;
}

源码地址:test.rar

展开阅读全文
打赏
0
3 收藏
分享
加载中

引用来自“糊涂圣”的评论

你好!我下载的源码,编译还是出现问题了,环境是windows xp sp3和vc 6.0 sp6
win32fixes.c
D:\redis-2.8\deps\test\win32fixes.c(150) : warning C4013: 'SetFilePointerEx' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4013: 'OpenThread' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4047: 'initializing' : 'void *' differs in levels of indirection from 'int '
D:\redis-2.8\deps\test\win32fixes.c(452) : warning C4305: '-=' : truncation from 'const unsigned __int64 ' to 'long '
Linking...
win32fixes.obj : error LNK2001: unresolved external symbol _SetFilePointerEx
win32fixes.obj : error LNK2001: unresolved external symbol _OpenThread
Debug/test.exe : fatal error LNK1120: 2 unresolved externals
这个问题是怎么解决的

引用来自“Injection”的评论

你还要安装sdk
我的sdk也安装了,是Microsoft Platform SDK for Windows XP SP2
2014/12/04 12:39
回复
举报
Injection博主

引用来自“糊涂圣”的评论

你好!我下载的源码,编译还是出现问题了,环境是windows xp sp3和vc 6.0 sp6
win32fixes.c
D:\redis-2.8\deps\test\win32fixes.c(150) : warning C4013: 'SetFilePointerEx' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4013: 'OpenThread' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4047: 'initializing' : 'void *' differs in levels of indirection from 'int '
D:\redis-2.8\deps\test\win32fixes.c(452) : warning C4305: '-=' : truncation from 'const unsigned __int64 ' to 'long '
Linking...
win32fixes.obj : error LNK2001: unresolved external symbol _SetFilePointerEx
win32fixes.obj : error LNK2001: unresolved external symbol _OpenThread
Debug/test.exe : fatal error LNK1120: 2 unresolved externals
这个问题是怎么解决的
你还要安装sdk
2014/12/04 12:07
回复
举报
你好!我下载的源码,编译还是出现问题了,环境是windows xp sp3和vc 6.0 sp6
win32fixes.c
D:\redis-2.8\deps\test\win32fixes.c(150) : warning C4013: 'SetFilePointerEx' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4013: 'OpenThread' undefined; assuming extern returning int
D:\redis-2.8\deps\test\win32fixes.c(250) : warning C4047: 'initializing' : 'void *' differs in levels of indirection from 'int '
D:\redis-2.8\deps\test\win32fixes.c(452) : warning C4305: '-=' : truncation from 'const unsigned __int64 ' to 'long '
Linking...
win32fixes.obj : error LNK2001: unresolved external symbol _SetFilePointerEx
win32fixes.obj : error LNK2001: unresolved external symbol _OpenThread
Debug/test.exe : fatal error LNK1120: 2 unresolved externals
这个问题是怎么解决的
2014/12/04 11:28
回复
举报
更多评论
打赏
3 评论
3 收藏
0
分享
返回顶部
顶部