由实例浅析C中的static、extern、multiple definition of用法及陷阱

2014-11-23 19:52:19 · 作者: · 浏览: 14

在JNI中,经常会遇到这种场景:想在一个.h文件中写一些全局变量,然后所有的cpp文件都能够使用。如下有个a.h文件:

/*
 * a.h
 *
 *  Created on: 2014-4-16
 *      Author: Administrator
 */

#ifndef A_H_
#define A_H_

int mAge = 0;
void setAge(int age);
int getAge();



#endif /* A_H_ */

里面很简单,有个变量mAge,两个接口,设置和读取。

下面是a.cpp文件:

/*
 * a.cpp
 *
 *  Created on: 2014-4-16
 *      Author: Administrator
 */

#include "a.h"
void setAge(int age){
	mAge = age;
}
int getAge(){
	return mAge;
}

然后有个main.cpp文件:

//============================================================================
// Name        : Test2.cpp
// Author      : yan
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include 
  
   
#include "a.h"
using namespace std;

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	setAge(22);
	cout<<"age = "<
   
    编译后报错:
    

E:\WorkSpaces\Eclipse_MinGW_C\Test2\Debug/../src/Test2.cpp:12: multiple definition of `main'
src\main.o:E:\WorkSpaces\Eclipse_MinGW_C\Test2\Debug/../src/main.cpp:13: first defined here
collect2.exe: error: ld returned 1 exit status
Build error occurred, build is stopped

报multiple definition of的错误,解决方法是将变量搞成static。搞成static确实不报错了,但这里却隐藏着一个天大的陷阱:

main.cpp如下:

#include 
     
      
#include "a.h"
using namespace std;

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	setAge(22);
	cout<<"age = "<
      




即虽然set成22了但是你访问的话,直接拿来用mAge仍然是初始化的0.而如果使用get接口来访问的数值是正确的。原因是:按书上说,static变量和一般全局变量一样,都存储在静态存储区。但static限定了变量访问权限,只能让本文件访问,为什么搞成static里,在main里有可以直接访问了?原因是static的变量是在h文件而非cpp文件中声明的。编译的过程中,include的东西类似直接覆盖的。所以直接访问mAge得到的是初始化的值,不光怎么set都没用。这着实是个陷阱啊!!!

事实上这样做也是不太安全的,更多时变量都在cpp文件声明。因此更加安全的方法是:

1、变量声明在cpp里,声明成普通的类型。然后通过set和get的方法进行设定和访问。

2、变量声明在cpp里,然后在h文件里将同样的变量声明成extern int mAge,这样所有包含h文件的既可以直接访问mAge。

3、static和extern是克星,一旦cpp文件里变量声明成static,那么在h文件里就不能extern了。

4、也可以h文件里声明普通变量,cpp里extern,但不推荐。变量还是在cpp里的好。

至少include “***.cpp”这种馊主意一般不要采用。

参考:

http://bbs.csdn.net/topics/390626289 page=1

http://blog.csdn.net/luo6620378xu/article/details/8511312





<script type=" text="" avascript"="">
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
分享到: 更多
<script type="text/java script" id="bdshare_js" data="type=tools&uid=12732"> <script type="text/java script" id="bdshell_js"> <script type="text/java script"> var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}}; document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000)