惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 探索

字符串倒序算法最优 - 探索 - 博客园 C++的基本概念和术语 关于软件汉化 郁闷! 命名空间语法 c++指针问题 - 探索 - 博客园 对程序集的理解 c#中重写(覆盖)和隐藏类的方法 最近好累呀~~~~ Vigenere加密算法类 人工智能规则正向演绎系统简单程序演示(c++) 爱情与婚姻 我的论坛刚刚建立起来,希望大家能支持一下~~~ oracle数据库中数据控制 初学者读书笔记数据库篇(一) C#中只允许产生一个类的实例的方法 今天申请了一个Gmail~~ 关于对SQL Server连接访问问题 今天罪孽深重~~~
extern关键字
探索 · 2006-06-09 · via 博客园 - 探索

   extern关键字的作用是声明变量和函数为外部链接,即该变量或函数名在其它文件中可见。用其声明的变量或函数应该在别的文件或同一文件的其它地方定义。

例如语句:extern int a;
    仅仅是一个变量的声明,其并不是在定义变量a,并未为a分配内存空间。变量a在所有模块中作为一种全局变量只能被定义一次,否则会出现连接错误。
    通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。例如,如果模块B欲引用该模块A中定义的全局变量和函数时只需包含模块A的头文件即可。这样,模块B中调用模块A中的函数时,在编译阶段,模块B虽然找不到该函数,但是并不会报错;它会在连接阶段中从模块A编译生成的目标代码中找到此函数。

如果一个工程包含如下两个文件:

1.cpp如下:                           2.cpp如下:

int x,y;                               extern int x,y;

extern void PrintHello();              void PrintHello()

void Func1()                           {

{                                          cout<<"hello!"<<endl; 

    x=123;                             }

}                                      void Func2()

int main()                             {

{                                          y=x*10;

    PrintHello();                      }   

    ......                             

}

   在2.cpp中使用extern int x,y;只是声明了x,y这两个变量,它告诉编译器其后的变量已经在别的文件中说明,不再为它们分配内存。当两个文件都编译成为.obj后,连接时所有的外部变量和函数都得到统一,可以共享各自定义的全局变量和函数。