Cmakelist.txt之Linux-redis配置

1.cmakelist.txt

复制代码
cmake_minimum_required(VERSION 3.16)
​
project(redis_linux_test LANGUAGES C)
​
​
​
add_executable(redis_linux_test main.c)
​
# 设置hiredis库的头文件路径和库文件路径
set(Hiredis_INCLUDE_DIR /usr/local/include/hiredis)
set(Hiredis_LIBRARY_DIR /usr/local/lib)
# 将hiredis的头文件路径添加到包含路径
include_directories(${Hiredis_INCLUDE_DIR})
# 将hiredis的库文件路径添加到库搜索路径
link_directories(${Hiredis_LIBRARY_DIR})
# 链接hiredis库到可执行文件
target_link_libraries(redis_linux_test hiredis)
​
​
include(GNUInstallDirs)
install(TARGETS redis_linux_test
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
复制代码

2.测试代码

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>

int main()
{
    // 创建Redis连接
    redisContext *c = redisConnect("192.168.127.128", 6379);
    if (c == NULL) {
        // 如果无法分配redis上下文,直接报错并退出
        printf("Can't allocate redis context\n");
        return 1;
    } else if (c->err) {
        // 如果连接有错误,打印错误信息并释放上下文,然后退出
        printf("Error: %s\n", c->errstr);
        redisFree(c);
        return 1;
    } else {
        // 连接成功时打印消息
        printf("Successfully connected to Redis server\n");
    }

    // 切换到数据库6
    redisReply *reply;
    reply = redisCommand(c, "SELECT 6");
    if (reply == NULL) {
        printf("Error executing SELECT command\n");
        redisFree(c);
        return 1;
    } else if (reply->type == REDIS_REPLY_ERROR) {
        printf("Error reply: %s\n", reply->str);
        freeReplyObject(reply);
        redisFree(c);
        return 1;
    }
    freeReplyObject(reply);

    // 插入一些数据,例如插入一个键值对 "name": "John"
    reply = redisCommand(c, "SET name John");
    if (reply == NULL) {
        printf("Error executing SET command\n");
        redisFree(c);
        return 1;
    } else if (reply->type == REDIS_REPLY_ERROR) {
        printf("Error reply: %s\n", reply->str);
        freeReplyObject(reply);
        redisFree(c);
        return 1;
    }
    freeReplyObject(reply);

    // 可以继续插入更多数据
    reply = redisCommand(c, "SET age 30");
    if (reply == NULL) {
        printf("Error executing SET command\n");
        redisFree(c);
        return 1;
    } else if (reply->type == REDIS_REPLY_ERROR) {
        printf("Error reply: %s\n", reply->str);
        freeReplyObject(reply);
        redisFree(c);
        return 1;
    }
    freeReplyObject(reply);

    redisFree(c);
    return 0;
}

3.结果

相关推荐
ambition202425 小时前
UNIX消息队列:从理论模型到工程实现的演进
linux·服务器·unix
阿正的梦工坊5 小时前
【Typescript】08-keyof-typeof-索引访问类型
linux·ubuntu·typescript
zhojiew6 小时前
使用Redis Stream订阅HUATUO发布SSE内核可观测性事件并进行AI分析的数据管道实践
运维·hbase·aws
轩Scott6 小时前
Ubuntu开机卡Logo?NVIDIA驱动修复全攻略
linux·ubuntu
Trouvaille ~6 小时前
【Redis篇】初识 Redis:特性、应用场景与版本演进
数据结构·数据库·redis·分布式·缓存·中间件·持久化
西柚小萌新6 小时前
【计算机常识】--Ngrok在Linux上内网穿透详细教程
linux·运维·服务器
Irissgwe6 小时前
一、网络基础概念
linux·网络·websocket·网络协议·socket·linux网络编程
施努卡机器视觉7 小时前
SNK施努卡铜箔包装线:从拔轴到入库,全流程自动化怎么实现?
运维·自动化
treesforest7 小时前
2026年,IP地理位置精准查询的几个硬核技术变化
运维·网络·网络协议·tcp/ip·ip
工业机器人销售服务7 小时前
在珠宝首饰加工中,遨博协作机器人配合微力控技术,实现宝石的自动化镶嵌
运维·机器人·自动化