Xcode 使用hiredis连接redis
1. 去https://github.com/redis/redis 下载redis源码
cd hiredis
make
make install
2. 打开xcode 创建一个xcode command line 工程
连接代码如下
#include <stdio.h>
#include <hiredis/hiredis.h>
int main(){
redisContext* conn = redisConnect("127.0.0.1", 6379);
if(conn->err)
printf("connection error:%s\n", conn->errstr);
redisReply* reply = (redisReply*) redisCommand(conn, "set hello world");
freeReplyObject(reply);
reply = (redisReply*) redisCommand(conn, "get hello");
printf("%s\n", reply->str);
freeReplyObject(reply);
redisFree(conn);
return 0;
}
此时没有配置头文件,和链接库会报错。
3. 打开工程的配置文件,对TARGETS进行配置(.xcodeproj)配置如下
在右边搜索框中搜索search
在Header Search Paths Debug中 添加 /usr/local/include/
在Library Search Paths Debug中添加/usr/local/lib/
此时依然不能运行,找不到库
4. 添加库
在工程配置文件中点击Build Phrases
在第三个Link Binary With Libraries中添加动态链接库
在Finder中按Command+Shift+G定位到/usr/local/lib
添加libhiredis静态或者动态链接库
ok,此时大功告成