Linux 开头双下划线的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10687053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:25:16  来源:igfitidea点击:

Meaning of double underscore in the beginning

clinuxgccx86-64glibc

提问by pythonic

In the standard library (glibc) I see functions defined with leading double underscores, such as __mmapin sys/mman.h. What is the purpose? And how can we still call a function mmapwhich doesn't seem to be declared anywhere. I mean we include sys/mman.hfor that, but sys/mman.hdoesn't declare mmap, it declares only __mmap.

在标准库 (glibc) 中,我看到使用前导双下划线定义的函数,例如__mmapsys/mman.h. 什么目的?我们如何仍然调用一个mmap似乎没有在任何地方声明的函数。我的意思是我们sys/mman.h为此包含,但sys/mman.h不声明mmap,它只声明__mmap.

采纳答案by dirkgently

From GNU's manual:

来自 GNU 的手册:

In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_') and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.

除了本手册中记录的名称之外,保留名称还包括以下划线 ('_') 开头的所有外部标识符(全局函数和变量)以及所有以两个下划线开头或一个下划线后跟大写字母是保留名称。这样库和头文件可以为内部目的定义函数、变量和宏,而不会有与用户程序中的名称冲突的风险。

This is a convention which is also used by C and C++ vendors.

这是 C 和 C++ 供应商也使用的约定。

回答by R.. GitHub STOP HELPING ICE

Names with leading double underscore are reserved for internal use by the implementation (compiler/standard library/etc.). They should never appear in your code. The purpose of this reserved namespace is to give the system headers names they can use without potentially clashing with names used in your program.

带有前导双下划线的名称保留供实现(编译器/标准库等)内部使用。它们永远不应该出现在您的代码中。这个保留命名空间的目的是为系统头文件提供它们可以使用的名称,而不会与程序中使用的名称发生潜在冲突。

回答by Lundin

ISO 9899:2011

ISO 9899:2011

7.1.3 Reserved identifiers

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.3 保留标识符

每个头声明或定义在其相关子条款中列出的所有标识符,并可选地声明或定义在其相关的未来库方向子条款中列出的标识符和标识符,这些标识符始终保留用于任何用途或用作文件范围标识符。

— 以下划线和大写字母或另一个下划线开头的所有标识符 始终保留供任何使用

— 所有以下划线开头的标识符始终保留用作普通名称空间和标记名称空间中具有文件范围的标识符。