Linux配置hiredis

Hiredisredis开源库对C语言接口的API开发库。


1. 初学者很容易搞混,redis-server redis-client都是应用程序,跟开发接口不相关。

http://redis.io/download 中介绍的

Installation

Download, extract and compile Redis with:

$ wget http://download.redis.io/releases/redis-2.8.3.tar.gz

$ tar xzf redis-2.8.3.tar.gz

$ cd redis-2.8.3

$ make

The binaries that are now compiled are available in the src directory. Run Redis with:

$ src/redis-server

You can interact with Redis using the built-in client:

$ src/redis-cli

redis> set foo bar

OK

redis> get foo

"bar"

也是服务端和客户端的安装与使用。


2. Hiredis 在官网 http://redis.io/clients 中有说明This is the official C client. Support for the whole command set, pipelining, event driven programming.

下载地址为https://github.com/redis/hiredis


3. Linux平台下载hiredis开发包,解压 

在命令行中 cd进入解压后的文件夹执行 Make 这样C客户端编译完毕。

接下来也是最关键的 配置。

可以手动配置 将对应.h.so .a等文件拷贝到/usr/local/lib /user/local/include对应文件夹

或者直接执行自动配置  make install  建议直接使用此方法。

至此hiredis开发包配置完毕


4. 编写程序

可以编写依赖hiredis 的程序了,

头文件需要包含 include <hiredis/hiredis.h>,然后编译的时候加上 -lhiredis

如果使用eclipse cdt 是在下图中对应加入链接库

Linux配置hiredis - December - My Blog
 

或者自己编写的makefile文件中加入    LDFLAGS = -lhiredis

-lhiredis 链接选项 这个设置当初让我很迷惑,

它其实解析出全程就是libhiredis.so,就是程序运行时加载libhiredis.so    redis的动态库

同理-lpthread 也就是libpthread.so

至此下述代码编译通过

#include <iostream>

using namespace std;

#include <hiredis/hiredis>

int main()

{

  cout<<"Hello World!!"<<endl;

  redisContext *conn = NULL;

  *conn = redisConnect("127.0.0.1", 6379); //redis server默认端口

  if(conn->err)

  {

  printf("connection error: %s", conn->str);

  }

}


5. 如果编译通过但是运行时候提示提示动态库加载失败

error while loading shared libraries XXXXX

出现这类错误表示,系统不知道xxx.so放在哪个目录下

默认系统配置识别/usr/lib 但是/usr/local/lib却没有被配置

解决办法/etc/ld.so.conf.d 文件夹内自己新建一个配置

Linux配置hiredis - December - My Blog
 

然后命令行下执行

/sbin/ldconfig –v更新一下配置即可


1.连接hiredis服务器
#include <stdio.h>
#include <hiredis/hiredis.h>


redisContext *conn = redisConnect("127.0.0.1", 6379); //redis server默认端口
if(conn->err){
printf("connection error: %s", conn->str);
}


2.发送命令至服务器
redisReply *reply = redisCommand(conn, "set key value");


3.关闭Reply对象
freeReplyObject(reply);


4.关闭连接
redisFree(conn);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值