关于hiredis库能否在windows系统下编译

有话要说

最近研究redis,在win32平台下的redis。
查阅了大量文章。
100%的文章都告诉我在windows平台(win32)下不能用hiredis。
我就怕了。
于是乎自己用socket根据redis协议写了一个redis链接库。
正常使用确实没问题。但由于redis协议的特点,tcp没办法知道redis服务端发过来的数据报文的长度,没有报头。
遇到网络状况不好时候,会造成接收数据不全,数据丢失,这问题就大了。
偶然间,我打开了hiredis的源代码。
看了一看hiredis里面的CMakeLists.txt文件。
发现了这样一段
在这里插入图片描述
我看见了WIN32
这特么hiredis明明是跨平台的,为什么都告诉我只能Linux?
我感觉被坑了。
尝试编译,配置好编译器,然后正常cmake,然后make,然后make install
目标文件夹下发现了编译好的动态链接库。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
放到qt里面测试一下。
先在pro文件里面添加头文件目录和动态链接库。
在这里插入图片描述
代码:

#include <QCoreApplication>
#include <QStringList>
#include <QDebug>
#include <QVector>
#include <QTcpSocket>
#include <QByteArray>
#include <iostream>
#include <hiredis/hiredis.h>
#include <QDebug>
#define cout qDebug()
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    redisContext *redis = redisConnect("xxx.xxx.xxx.xxx", 6379);
        if(redis == nullptr || redis->err){
            cout << redis->errstr << endl;
            return -1;
        }else{
            cout << "connect successfully" << endl;
        }
        QString key1 = QString("china_新增治愈").toUtf8();
        qDebug() << key1;
        redisReply * reply = (redisReply*)redisCommand(redis, "auth root");
        cout << reply->str << endl;
        redisReply * reply1 = (redisReply*)redisCommand(redis, QString("hgetall "+key1).toStdString().c_str());
        if (redis->err){
            cout << redis->errstr << endl;
        } else{
            cout <<"type:" << reply1->type << endl;
            cout << "size:" << reply1->elements << endl;
            for(int i = 0; i < reply1->elements; i++){
                cout << reply1->element[i]->str << endl;
            }
        }

    return a.exec();
}

为什么用qt呢,因为我的redis里面键名存在中文,用qt处理utf-8编码方便一些。
测试成功。
在这里插入图片描述

以后谁在跟我说hiredis不能在windows下用,就一巴掌拍过去。

附带一些编译参数

1、hiredis的CMakeLists.txt文件CMAKE_MINIMUM_REQUIRED之前加上:

set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-fexec-charset=GBK -finput-charset=UTF-8")

用来指定编译器和字符集
2、cmake命令行参数

cmake . -G "MinGW Makefiles" -D "CMAKE_MAKE_PROGRAM:PATH=make.exe的绝对地址" -B ./build/release

-B后面为cmake编译后存放的位置。
然后进到这个位置
make
make install
默认install的位置是系统盘内Program Files(x86)文件夹的hiredis内,翻一下就能看见了。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值