您的位置:首页 > 游戏 > 手游 > 最新在线免费网站_化妆品备案查询_网站服务器ip查询_南宁seo推广服务

最新在线免费网站_化妆品备案查询_网站服务器ip查询_南宁seo推广服务

2024/12/26 20:12:31 来源:https://blog.csdn.net/qq_52646857/article/details/143964380  浏览:    关键词:最新在线免费网站_化妆品备案查询_网站服务器ip查询_南宁seo推广服务
最新在线免费网站_化妆品备案查询_网站服务器ip查询_南宁seo推广服务

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_testLIBRARY 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");}// 切换到数据库6redisReply *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.结果

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com