项目属性中包含一下头文件和库文件的目录
这三项若能正常运行则不用加 若有问题 看着调试吧…这里已经涉及到知识盲区了
- 项目->属性->配置属性->C/C+±>代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)
- 项目->属性->配置属性->链接器->命令行中输入/NODEFAULTLIB:libcmt.lib
- 项目->属性->配置属性->C/C+±>预处理器->预处理器定义->添加“_CRT_SECURE_NO_WARNINGS”
代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <hiredis.h>
#include "./Win32_Interop/win32fixes.h"
#define NO_QFORKIMPL
#include <Win32_Interop/win32fixes.h>
#pragma comment(lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib")
int main(int argc, char** argv) {
//链接 redis server
struct timeval timeout = { 1, 500000 }; // 1.5 秒 超时 {秒,微秒}
redisContext* c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);
if (c->err) {
//超时 链接错误
printf("Connection error: %s\n", c->errstr);
goto end;
}
//操作
//若有错误 返回null
redisReply* replay = redisCommand(c, "select %d", 15);
if (replay) {
printf("%d, %s\n", replay->type, replay->str);
freeReplyObject(replay);
}
else {
printf("relay == NULL");
}
replay = redisCommand(c, "zadd world_rank 3000 xt");
if (replay) {
printf("%d %d\n", replay->type, replay->integer);
freeReplyObject(replay);
}
replay = redisCommand(c, "zrange world_rank 0 10 withscores");
if (replay) {
if (replay->type == REDIS_REPLY_ARRAY) {
for (int i = 0; i < replay->elements; i++) {
printf("%d: %s\n", i, replay->element[i]->str);
}
}
freeReplyObject(replay);
}
end:
//释放
redisFree(c);
system("pause");
return 0;
}