码迷,mamicode.com
首页 > 其他好文 > 详细

#ifndef still cannot garantee .h only be compiled once?

时间:2016-06-03 18:58:34      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

-------------example---------------

-------------ah.h-----------------

#ifndef _AH_H

#define _AH_H

 int a=5;

#endif

 

-------------calla.cpp-------------

#include"ah.h"

 

 void seta()

{

   a=2;

}

 

------------main-------

#include"ah.h"

#include<iostream>

 

void seta();

using namespace std;

void main()

{   

   cout<<"a="<<a<<endl;  

   seta();  

   cout<<"after seta, a="<<a<<endl;

}

 

==================================================

1>------ 已启动生成: 项目: taextern, 配置: Debug Win32 ------
1>生成启动时间为 2016/6/3 星期五 下午 13:40:55。
1>InitializeBuildStatus:
1>  正在创建“Debug\taextern.unsuccessfulbuild”,因为已指定“AlwaysCreate”。
1>ClCompile:
1>  所有输出均为最新。
1>ManifestResourceCompile:
1>  所有输出均为最新。
1>calla1.obj : error LNK2005: "int a" (?a@@3HA) 已经在 tmain.obj 中定义
1>d:\userdata\administrator\documents\visual studio 2010\Projects\taextern\Debug\taextern.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
1>
1>生成失败。
1>
1>已用时间 00:00:00.13
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

 

 

 

----------------------Example 2---------------

---------set global variable---------

---------set extern -------------

 

==================================================

--------------ah.h--------------

#ifndef _AH_H

#define _AH_H

 int a=5;

#endif

 

--------------calla.cpp-----------

 

//#include"ah.h"

extern int a;

 void seta()

{  

a=2;

}

 

-------------main----------------

#include"ah.h"

#include<iostream>

void seta();

using namespace std;

void main()

{  

cout<<"a="<<a<<endl;  

seta();  

cout<<"after seta, a="<<a<<endl;

}

 

-----------------------------------------------------------

OK.

 

#ifndef still cannot garantee .h only be compiled once?

标签:

原文地址:http://www.cnblogs.com/stevenxiu/p/5557113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!