在Redis2.8中有networking.c,这个文件没有networking.h
networking.c首先引入redis.h这个头文件
#include "redis.h"
在redis.c一开始就声明了全局变量
/* Global vars */
struct redisServer server;
networking.c的createClient函数
redisClient *createClient(int fd) {
redisClient *c = zmalloc(sizeof(redisClient));
/* passing -1 as fd it is possible to create a non connected client.
* This is useful since all the Redis commands needs to be executed
* in the context of a client. When commands are executed in other
* contexts (for instance a Lua script) we need a non connected client. */
if (fd != -1) {
anetNonBlock(NULL,fd);
anetEnableTcpNoDelay(NULL,fd);
if (server.tcpkeepalive)
anetKeepAlive(NULL,fd,server.tcpkeepalive);
if (aeCreateFileEvent(server.el,fd,AE_READABLE,
readQueryFromClient, c) == AE_ERR)
{
close(fd);
zfree(c);
return NULL;
}
}
这里之所以可以引用redisClient就是因为redisClient在redis.h是被声明为 extern的,而networking.c已经引入redis.h这个头文件
/*-----------------------------------------------------------------------------
* Extern declarations
*----------------------------------------------------------------------------*/
extern struct redisServer server;
不然注释掉extern struct redisServer server是编译不过去的:
关于extern和头文件的解释:
出自《C语言入门经典(第四版)》
出自《21天学通C语言(第6版)》
出自《C语言编程:一本全面的C语言入门教程 (第3版)》