1. Windows平台下如何编译C++版本的Redis库hiredis

    Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

    Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。

    Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

    Redis的官网地址,非常好记,是redis.io。(域名后缀io属于国家域名,是british Indian Ocean territory,即英属印度洋领地),Vmware在资助着redis项目的开发和维护。

    从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。

1、下载redis源码

GitHub - microsoftarchive/redis: Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes

2、编译代码

    推荐使用VS2013,打开目录中的redis-3.0\msvs\RedisServer.sln,编译即可,如果提示以下错误,需要适当修改代码,后面的依次类推:

1>..\..\src\ae.c(187): error C2275: “aeFileEvent”: 将此类型用作表达式非法
1>          c:\users\administrator\desktop\redis-3.0\src\ae.h(67) : 参见“aeFileEvent”的声明
1>..\..\src\ae.c(187): error C2065: “fe”: 未声明的标识符
1>..\..\src\ae.c(189): error C2065: “fe”: 未声明的标识符
1>..\..\src\ae.c(189): error C2223: “->mask”的左侧必须指向结构/联合

比如编译会出现报错的函数:

int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
    if (fd >= eventLoop->setsize) return 0;
    aeFileEvent *fe = &eventLoop->events[fd];

    return fe->mask;
}

由于C文件编译器中限定变量需要放在最前面函数体中间不允许临时定义变量,故参照下面的方式修改即可:

int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
	aeFileEvent *fe = NULL;
    if (fd >= eventLoop->setsize) return 0;
    fe = &eventLoop->events[fd];

    return fe->mask;
}

修改完毕后重新编译即可!

1. Windows平台下如何编译C++版本的Redis库hiredis_第1张图片

3、生成静态lib文件

    生成的文件位于:redis-3.0\msvs\Win32\Release,如下图,在后续工程中只需要引用此LIB即可在VS C++环境中编译你的应用:

1. Windows平台下如何编译C++版本的Redis库hiredis_第2张图片

4、运行Redis服务端:

    先将配置文件redis-3.0\redis.conf拷贝上面的EXE所在目录:

1. Windows平台下如何编译C++版本的Redis库hiredis_第3张图片

通常只需要修改redis.conf中的几个常用参数

# 侦听端口(默认)
port 6379

# 数据库本地的文件名
dbfilename dump.rdb

# 修改访问密码
################################## SECURITY ###################################
requirepass admin

最后在命令行运行 redis-server.exe redis.conf 即可启动服务端:

1. Windows平台下如何编译C++版本的Redis库hiredis_第4张图片

下一章节将讲解在Redis常见命令及对比MySQL细节!

注意:

由于hiredis是在VS2013编译生成的,否则测试工程必须要用VS2013或以上的版本,否则会提示下面的错误:

1>  所有输出均为最新。
1>MSVCRTD.lib(ti_inst.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(xlock.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(stdthrow.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(xthrow.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(uncaught.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(locale0.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(xdebug.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(ios.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(cerr.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(iosptrs.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>libcpmtd.lib(xmutex.obj) : error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”(Win32_Interop.lib(Win32_FDAPI.obj) 中)
1>LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
1>hiredis.lib(hiredis.obj) : error LNK2019: 无法解析的外部符号 ___report_rangecheckfailure,该符号在函数 _redisvFormatCommand 中被引用
1>Win32_Interop.lib(Win32_Error.obj) : error LNK2001: 无法解析的外部符号 ___report_rangecheckfailure
1>Win32_Interop.lib(Win32_FDAPI.obj) : error LNK2001: 无法解析的外部符号 ___report_rangecheckfailure
1>Win32_Interop.lib(Win32_ANSI.obj) : error LNK2001: 无法解析的外部符号 ___report_rangecheckfailure
1>hiredis.lib(hiredis.obj) : error LNK2019: 无法解析的外部符号 __vacopy,该符号在函数 _redisvFormatCommand 中被引用
1>hiredis.lib(sds.obj) : error LNK2001: 无法解析的外部符号 __vacopy
1>Win32_Interop.lib(Win32_FDAPI.obj) : error LNK2019: 无法解析的外部符号 "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ),该符号在函数 "char * __cdecl std::_Allocate(unsigned int,char *)" (??$_Allocate@D@std@@YAPADIPAD@Z) 中被引用
1>Win32_Interop.lib(win32_rfdmap.obj) : error LNK2001: 无法解析的外部符号 "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ)
1>Win32_Interop.lib(Win32_variadicFunctor.obj) : error LNK2001: 无法解析的外部符号 "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ)
1>Win32_Interop.lib(Win32_EventLog.obj) : error LNK2001: 无法解析的外部符号 "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ)
1>Win32_Interop.lib(Win32_FDAPI.obj) : error LNK2019: 无法解析的外部符号 "char const * __cdecl std::_Syserror_map(int)" (?_Syserror_map@std@@YAPBDH@Z),该符号在函数 "public: virtual class std::error_condition __thiscall std::_System_error_category::default_error_condition(int)const " (?default_error_condition@_System_error_category@std@@UBE?AVerror_condition@2@H@Z) 中被引用
1>Win32_Interop.lib(Win32_variadicFunctor.obj) : error LNK2001: 无法解析的外部符号 "char const * __cdecl std::_Syserror_map(int)" (?_Syserror_map@std@@YAPBDH@Z)
1>Win32_Interop.lib(Win32_EventLog.obj) : error LNK2001: 无法解析的外部符号 "char const * __cdecl std::_Syserror_map(int)" (?_Syserror_map@std@@YAPBDH@Z)
1>Win32_Interop.lib(Win32_FDAPI.obj) : error LNK2019: 无法解析的外部符号 "char const * __cdecl std::_Winerror_map(int)" (?_Winerror_map@std@@YAPBDH@Z),该符号在函数 "public: virtual class std::basic_string,class std::allocator > __thiscall std::_System_error_category::message(int)const " (?message@_System_error_category@std@@UBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@H@Z) 中被引用
1>Win32_Interop.lib(Win32_variadicFunctor.obj) : error LNK2001: 无法解析的外部符号 "char const * __cdecl std::_Winerror_map(int)" (?_Winerror_map@std@@YAPBDH@Z)
1>Win32_Interop.lib(Win32_EventLog.obj) : error LNK2001: 无法解析的外部符号 "char const * __cdecl std::_Winerror_map(int)" (?_Winerror_map@std@@YAPBDH@Z)
1>Win32_Interop.lib(Win32_EventLog.obj) : error LNK2019: 无法解析的外部符号 "void __cdecl std::_Facet_Register(class std::_Facet_base *)" (?_Facet_Register@std@@YAXPAV_Facet_base@1@@Z),该符号在函数 "class std::ctype const & __cdecl std::use_facet >(class std::locale const &)" (??$use_facet@V?$ctype@D@std@@@std@@YAABV?$ctype@D@0@ABVlocale@0@@Z) 中被引用
1>Win32_Interop.lib(Win32_EventLog.obj) : error LNK2019: 无法解析的外部符号 "private: static class std::locale::_Locimp * __cdecl std::locale::_Init(bool)" (?_Init@locale@std@@CAPAV_Locimp@12@_N@Z),该符号在函数 "public: __thiscall std::locale::locale(void)" (??0locale@std@@QAE@XZ) 中被引用
1>Win32_Interop.lib(Win32_Time.obj) : error LNK2019: 无法解析的外部符号 __dtoul3,该符号在函数 _GetHighResRelativeTime 中被引用
1>Win32_Interop.lib(Win32_Time.obj) : error LNK2019: 无法解析的外部符号 __ltod3,该符号在函数 _GetHighResRelativeTime 中被引用
1>C:\Users\Administrator\Desktop\RedisTest\Debug\RedisTest.exe : fatal error LNK1120: 9 个无法解析的外部命令
1>
1>生成失败。
1>
1>已用时间 00:00:01.53
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

避坑指南:这个版本似乎不太稳定,主进程EXE这些推荐使用官方的版本,在此我们只需要用它的lib库即可!

你可能感兴趣的:(ASM/WTL/MFC/QT,redis,数据库,C++,排名系统,排行榜)