在C中包含保护约定 [英] Include guard conventions in C

查看:98
本文介绍了在C中包含保护约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置包括防护的常规方法是什么?我通常将它们写为(例如.h):

What is the conventional way to set up your include guards? I usually write them as (for example.h):

#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_
#include "example.h"
#endif

下划线约定重要吗?我在Google上搜索时看到了相互矛盾的信息. _EXAMPLE_H_甚至必须匹配标题的名称吗?

Does underscore convention matter? I've seen conflicting information when I googled this. Does the _EXAMPLE_H_ even have to match the name of the header?

推荐答案

Does underscore convention matter?

是的.这很重要.

带有下划线后跟大写字母的标识符保留给实现.因此,您所拥有的将导致不确定的行为.

Identifiers with a leading underscore followed upper case letter is reserved for implementation. So what you have would cause undefined behaviour.

以下是用于命名标识符的C标准规范(

The following is the C standard's specification for naming the identifiers (C11 draft):

7.1.3保留标识符

每个标头声明或定义在其标头中列出的所有标识符 相关的条款,并可选地声明或定义标识符 在相关的未来图书馆指南条款中列出,并且 始终保留用于任何用途或用作以下用途的标识符 文件范围标识符.

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

-所有以下划线开头且以 大写字母或其他下划线始终保留给任何 使用.

— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

-始终保留所有以下划线开头的标识符 用作普通和标签中文件范围的标识符 命名空间.

— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

-以下任何子节中的每个宏名称(包括 如果有以下任何一种情况,则保留按指定用途使用) 它的相关头包括在内;除非另有明确说明 (请参见7.1.4). —在任何以下内容中具有外部链接的所有标识符 遵循以下条款(包括将来的图书馆指南),以及 errno始终保留用作外部的标识符 links.184)—每个文件中列出的每个文件范围的标识符 以下条款(包括将来的图书馆说明)是 保留用作宏名和文件范围的标识符 如果包含任何相关联的标头,则在同一个名称空间中.

— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4). — All identifiers with external linkage in any of the following subclauses (including the future library directions) and errno are always reserved for use as identifiers with external linkage.184) — Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

没有其他标识符保留.如果程序声明或定义 保留它的上下文中的标识符(不是 7.1.4允许),或将保留的标识符定义为宏名称, 行为是不确定的.

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

如果程序删除(使用#undef) 上面列出的第一组中的标识符,则行为未定义.

If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

在不违反上述任何规定的情况下,include保护名称可以是任何名称,而不必是头文件的名称.但是通常我看到/使用的约定是使用与头文件名相同的名称,以免引起不必要的混乱.

Without violating any of the above, the include guard name can be anything and doesn't have to be the name of the header file. But usually the convention I have seen/used is to use same name as that of the header file name so that it doesn't cause any unnecessary confusion.

这篇关于在C中包含保护约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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