打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C语言extern关键字定义外部变量

http://blog.csdn.net/unix21/article/details/15337939

2013

在Redis2.8中有networking.c,这个文件没有networking.h

networking.c首先引入redis.h这个头文件

  1. #include "redis.h"  


在redis.c一开始就声明了全局变量

  1. /* Global vars */  
  2. struct redisServer server;  

networking.c的createClient函数

  1. redisClient *createClient(int fd) {  
  2.     redisClient *c = zmalloc(sizeof(redisClient));  
  3.   
  4.     /* passing -1 as fd it is possible to create a non connected client. 
  5.      * This is useful since all the Redis commands needs to be executed 
  6.      * in the context of a client. When commands are executed in other 
  7.      * contexts (for instance a Lua script) we need a non connected client. */  
  8.     if (fd != -1) {  
  9.         anetNonBlock(NULL,fd);  
  10.         anetEnableTcpNoDelay(NULL,fd);  
  11.         if (server.tcpkeepalive)  
  12.             anetKeepAlive(NULL,fd,server.tcpkeepalive);  
  13.         if (aeCreateFileEvent(server.el,fd,AE_READABLE,  
  14.             readQueryFromClient, c) == AE_ERR)  
  15.         {  
  16.             close(fd);  
  17.             zfree(c);  
  18.             return NULL;  
  19.         }  
  20.     }  

这里之所以可以引用redisClient就是因为redisClient在redis.h是被声明为extern的,而networking.c已经引入redis.h这个头文件

  1. /*----------------------------------------------------------------------------- 
  2.  * Extern declarations 
  3.  *----------------------------------------------------------------------------*/  
  4.   
  5. extern struct redisServer server;  


不然注释掉extern struct redisServer server是编译不过去的:


关于extern和头文件的解释:


出自《C语言入门经典(第四版)》



出自《21天学通C语言(第6版)》


出自《C语言编程:一本全面的C语言入门教程 (第3版)》

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Redis主体流程分析
redis源代码分析–event library
c/c 中的一些基础但必须熟记的知识
C++ 全局变量、静态全局变量和静态局部变量的异同?能结合代码详细分析一下吗?谢谢 - 已...
net core使用redis基于StackExchange.Redis教程
分布式缓存扩展Session机制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服