三个#include卫兵,永远不会破坏? [英] Three #include guards, which will never break?

查看:62
本文介绍了三个#include卫兵,永远不会破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时程序员会在命令行定义宏,如:


$ gcc -DF1_H ...

$ gcc -DF1_H = 0 ...

$ gcc -DF1_H = 1 ...


标记为#1,#2,#3的以下三行中的一行可提供#include

保护和避免错误可能由冗余#include引入。

每个人都有任何缺陷吗?哪一个永远不会破坏?


#if F1_H / *#1 * /

/ * #if!defined(F1_H)* / / *#2 * /

/ * #ifndef F1_H * / / *#3 * /

#define F1_H


/ *更多东西在这里... * /


#endif

-

lovecreatesbeauty

解决方案

gcc -DF1_H ...


gcc -DF1_H = 0 ...

gcc -DF1_H = 1 ...

标记为#1,#2,#3的以下三行之一可提供#include

保护和避免错误可能由冗余#include引入。

每个人都有任何缺陷吗?哪一个永远不会破坏?


#if F1_H / *#1 * /

/ * #if!defined(F1_H)* / / *#2 * /

/ * #ifndef F1_H * / / *#3 * /

#define F1_H


/ *更多东西在这里... * /


#endif

-

lovecreatesbeauty


Sometimes programmers will define macros at command line like:

$ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ...

One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break?

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif
--
lovecreatesbeauty

解决方案

gcc -DF1_H ...


gcc -DF1_H=0 ...


gcc -DF1_H=1 ...

One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break?

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif
--
lovecreatesbeauty


这篇关于三个#include卫兵,永远不会破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆