windows下hiredis的编译

将msvc项目中C/C++->运行库->MTD改为MDD再编译,要不静态连接能用多少别的库。

转自windows下hiredis的编译

编译环境,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”(不带引号)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值