您的位置:首页 > 编程语言 > C语言/C++

C++中extern关键字使用

2017-05-24 15:49 330 查看
extern关键字的作用 

extern是一个关键字,它告诉编译器存在着一个变量或者一个函数,如果在当前编译语句的前面中没有找到相应的变量或者函数,也会在当前文件的后面或者其它文件中定义,来看下面的例子。

// extern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

extern int i;
extern void func();
int _tmain(int argc, _TCHAR* argv[])//typedef wchar_t _TCHAR;#define _tmain wmain
{
i = 0;
func();
return 0;
}

int i;

void func()
{
i++;
cout << "i = " << i << endl;
}

上面代码中变量i和函数func在文件末尾定义,所以变量需要使用extern关键字告诉编译器,变量在别的地方定义。extern int i我原来以为extern i就可以,结果编译器报错,仔细想下确实应该,否则编译器不知道i是什么类型的数据,又怎么能判断i = 0是否是一个正确的赋值语句呢?
 

    那么定义在其他文件中的函数和变量,如何通过extern关键字调用呢?

    首先,定义在其它文件中的函数和变量,可以使用两种方法调用:

        一、使用头文件调用,这时候,函数和变量必须在头文件中定义和声明。

        二、使用extern关键字调用,这时候函数和变量在.cpp或者.c文件中定义和声明。

    看下面两个例子:

    devVar.cpp函数中定义:

#include "stdafx.h"

int i;

extern.cpp中
// extern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

extern int i;
extern void func();
int _tmain(int argc, _TCHAR* argv[])//typedef wchar_t _TCHAR;#define _tmain wmain
{
i = 0;
func();
return 0;
}

void func()
{
i++;
cout << "i = " << i << endl;
}



编译工程,程序输出:i = 1,这里使用extern关键字声明在其它cpp文件中定义的变量和函数。
 

    #include <filensme> --- 将filename文件中的内容插入到新的文件中。

    deVar.h文件中代码为

#include <stdio.h>

int i = 1;

void func()
{
printf("%d",i++);
}


函数func修改全局变量i的值并输出。
    extern.cpp文件内容为:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#include "devVar.h"
//extern int i;
//extern void func();

int main(void)
{
for (int x = 0;x < 10; x++)
{
func();
}
}

程序输出1,2,3,4,5,6,7,8,9,10,这里#include <filname.h> 包含定义在其它头文件中的函数和变量,在来看一个例子。

// extern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

extern int i;
extern int  func(int);//这里extern必需的,函数定义在其它cpp文件中
int _tmain(int argc, _TCHAR* argv[])//typedef wchar_t     _TCHAR;#define _tmain      wmain
{
i = 100;
func(i);
return 0;
}


devVar.cpp文件中内容为:
#include "stdafx.h"
#include <iostream>
using namespace std;

int i;

int func(int a)
{
i = a;
cout << "i = " << i << endl;
return 0;
}

这样,同样是输出了i= 100。
    能够使用extern引用其它cpp文件中定义的函数说明了一个问题:

    如果一个工程现编译cpp文件,在把多个目标文件链接成为可执行文件,而两个或多个文件中,定义了相同的全局变量,那么,程序编译的时候不会报错,因为编译器单独编译每个文件,在链接可执行文件的时候,由于多个目标文件中含有相同的全局变量,而生成可执行文件的时候,任何文件中定义的全局变量对其它目标文件都是可见的,此时由于变量定义冲突而发生错误。看下面的代码:

    // extern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int i;
extern int func(int);//这里extern是必须的函数定义在别的cpp文件中
int _tmain(int argc, _TCHAR* argv[])//typedef wchar_t _TCHAR;#define _tmain wmain { i = 100; func(i); return 0; }


devVar.cpp文件中,内容为:
#include "stdafx.h"
#include <iostream>
using namespace std;

int i;

int func(int a)
{
i = a;
cout << "i = " << i << endl;
return 0;
}

单独compile任何一个cpp文件都是对的,但是 编译工程,生成可执行文件的时候报错:
    1>LINK : D:\vctest\extern\Debug\extern.exe not found or not built by the last incremental link; performing full link

1>devVar.obj : error LNK2005: "int i" (?i@@3HA) already defined in extern.obj

1>D:\vctest\extern\Debug\extern.exe : fatal error LNK1169: one or more multiply defined symbols found

    原因是:两个.cpp文件中都定义了全局变量i,变量重复定义了。

 

    PS:定义在.h文件中的函数和变量不能使用extern变量声明,原因是#include <filename>在预编译的时候将.h文件中的内容插入了cpp文件中,因此编译器找得到在其它.h文件中定义的变量或者函数。编译的时候,只编译cpp文件的内容,.h文件时不参与编译,如果使用extern声明在.h文件中定义的变量或者函数,那么声明为extern的变量和函数在其它.cpp文件中找不到,因此程序编译的时候就发生了错误。

原文地址:http://blog.csdn.net/sruru/article/details/7951019
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: