hiredis文件在redis的deps文件夹中
进入deps文件夹中
运行将hiredis文件夹移动/usr/lib
cp -r hiredis/ /usr/lib
进入/usr/lib/hiredis后对其进行编译和配置动态库
cd /usr/lib/hireids
make
make install
ldconfig
在添加文件
vim /etc/ld.so.conf.d/usr-libs.conf
输入
/usr/local/lib
更新配置
/sbin/ldconfig
创建文件
vim main.c
输入
#include <stdio.h>
#include <hiredis/hiredis.h>
int main()
{
redisContext *connect = redisConnect("127.0.0.1",6379);
if(connect != NULL && connect->err)
{
printf("connection error: %s\n",connect->errstr);
return 0;
}
redisReply *reply = (redisReply*)redisCommand(connect,"set test 001");
freeReplyObject(reply);
reply = redisCommand(connect,"get test");
printf("%s\n",reply->str);
freeReplyObject(reply);
redisFree(connect);
return 0;
}
编译文件后运行
gcc -o main main.c -L/usr/local/lib/ -lhiredis
./main
出现结果