天天看點

windows下hiredis的編譯



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