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

C++中extern关键字使用(转)

2015-08-18 16:05 267 查看
参考文章:http://blog.csdn.net/sruru/article/details/7951019

chapter1、如何混合编译C语言和C++

实际开发过程中,C++中会调用C与语言编写的代码,我在网络上面找到一篇写得很好的文章

http://blog.csdn.net/keensword/article/details/401114

方法一、全局函数和变量在devVar.c文件中实现,在extern.cpp文件中使用extern关键字声明在devVar.c文件中定义的函数和变量。

devVar.c文件的代码如下所示:

#include <stdio.h>

int i = 1;

void func()

{

printf("%d",i++);

}

extern.cpp文件中代码如下所示:

#include "stdafx.h"

#include <stdio.h>

#include <iostream>

using namespace std;

//#include "devVar.h"

//extern int i;

//extern void func();

extern "C"

{

extern int i;

extern void func();

//#include "devVar.h"

}

int main(void)

{

for (int x = 0;x < 10; x++)

{

func();

}

}

所以在C++文件中编译C文件需要使用extern "C"关键字,声明语法如下所示

extern "C"

{

采用C语言实现的内容

}

方法二、

在devVar.h文件中实现C代码(即devVar.h作为C语言头文件),在.cpp文件中包含C语言头文件。

devVar.h头文件内容为:

#include <stdio.h>

int i = 1;

void func()

{

printf("%d",i++);

}

extern.cpp文件内容如下所示

#include "stdafx.h"

#include <stdio.h>

#include <iostream>

using namespace std;

//#include "devVar.h"

//extern int i;

//extern void func();

extern "C"

{

//extern int i;

//extern void func();

#include "devVar.h"

}

int main(void)

{

for (int x = 0;x < 10; x++)

{

func();

}

}

其中,包含C语言头文件的方式为:

extern "C"

{

//extern int i;

//extern void func();

#include "devVar.h"

}

写到这里,楼主又产生了一个疑问,上面的例子讲的是C++调用C实现的代码,那如果是C调用C++编写的代码呢?

楼主作了如下改动:

devVar.cpp代码为:

#include <stdio.h>

int i = 1;

void func()

{

printf("%d",i++);

}

extern.c文件代码为

#include <stdio.h>

extern int i;

extern void func();

int main(void)

{

int x = 0;

for (;x < 10; x++)

{

func();

}

}

单独编译每个文件都通过,链接声称可执行文件的时候报错:

1>extern.obj : error LNK2019: unresolved external symbol _func referenced in function _main,说明.c文件中extern void func(),按照C编译的规则,得到函数_func,而devVar.cpp文件采用C++编译方式,得到的函数为XX·!_func(具体楼主也不知道哈),这样链接的时候函数自然找不到,那怎么解决呢?

需要在devVar.cpp中,明确调用extern "C"关键字,声明cpp文件中有关代码,需要按照C的方式来生成,修改devVar.cpp文件如下所示:

#include <stdio.h>

int i = 1;

extern "C" void func()

{

printf("%d",i++);

}

此时,除了需要使用extern "C"声明编译的时候采用C方式编译外,.cpp文件中的代码可以按照C++方式编写,例如

devVar.cpp按照下面方式写,也是正确的。

#include "stdafx.h"

#include <iostream>

using namespace std;

int i = 1;

extern "C" void func()

{

cout << "i = " << i++ << endl;

chapter 2 . 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文件中找不到,因此程序编译的时候就发生了错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: