windows下hiredis的编译

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: windows下hiredis的编译,有需要的朋友可以参考下。果然,高端的程序员真心是鸟都不鸟windows的,redis的客户端找了一圈愣是没有C++的windows版本我要做个windows上的C++的服务...


windows下hiredis的编译,有需要的朋友可以参考下。




果然,高端的程序员真心是鸟都不鸟windows的,redis的客户端找了一圈愣是没有C++的windows版本


我要做个windows上的C++的服务器都没办法和redis交互


github上所有能试的我都试过了,要么是只支持unix,要么是怎么编译都不通过,焦头烂额中


然后我总结了网上无数的教程,附带修复一个个编译错误,总结如下



编译环境,64位windows7 ultimate,VS2013 Ultimate



1.获取redis windows版


MS Open Technologies 官方主页


GitHub上的MSOpenTech/redis项目地址



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”(不带引号)

目录
相关文章
|
关系型数据库 MySQL 开发工具
windows编译poco c++库
windows编译poco c++库
|
Go Windows
Go 交叉编译 跨平台编译(windows系统跨平台编译)
Go 交叉编译 跨平台编译(windows系统跨平台编译)
210 0
|
C++ Windows
masscan for windows windows编译masscan
masscan for windows windows编译masscan
660 0
masscan for windows windows编译masscan
|
Linux 编译器 数据安全/隐私保护
Windows10 使用MSYS2和VS2019编译FFmpeg源代码-测试通过
FFmpeg作为一个流媒体的整体解决方案,在很多项目中都使用了它,如果我们也需要使用FFmpeg进行开发,很多时候我们需要将源码编译成动态库或者静态库,然后将库放入到我们的项目中,这样我们就能在我们的项目中使用FFmpeg提供的接口进行开发。关于FFmpeg的介绍这里就不过多说明。
770 0
|
Unix 编译器 开发者
Qt5.14.2 轻松掌握Qt中的压缩与解压缩:QuaZIP的神秘面纱与实战演练之windows环境编译
Qt5.14.2 轻松掌握Qt中的压缩与解压缩:QuaZIP的神秘面纱与实战演练之windows环境编译
1218 0
|
JSON 开发工具 C++
Windows下用CMake编译JsonCpp及配置(适合新手)
Windows下用CMake编译JsonCpp及配置(适合新手)
1299 0
|
API PHP C++
Windows下用vs2017编译和配置libcurl库(手把手教,适合新人)
Windows下用vs2017编译和配置libcurl库(手把手教,适合新人)
2331 0
|
监控 机器人 Unix
GoLand——windows下如何编译Linux二进制文件
GoLand——windows下如何编译Linux二进制文件
193 1
GoLand——windows下如何编译Linux二进制文件
|
并行计算 开发工具 异构计算
在Windows平台使用源码编译和安装PyTorch3D指定版本
【10月更文挑战第6天】在 Windows 平台上,编译和安装指定版本的 PyTorch3D 需要先安装 Python、Visual Studio Build Tools 和 CUDA(如有需要),然后通过 Git 获取源码。建议创建虚拟环境以隔离依赖,并使用 `pip` 安装所需库。最后,在源码目录下运行 `python setup.py install` 进行编译和安装。完成后即可在 Python 中导入 PyTorch3D 使用。
1170 0
|
C++ Windows
Windows下编译64位CGAL
Windows下编译64位CGAL
447 0

热门文章

最新文章