预处理器指令
预处理器指令到底是什么?我确实知道 #include
是一个预处理器指令,但它到底有什么作用?
What exactly is a preprocessor directive? I do have an idea that #include
is a preprocessor directive but what does it exactly do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
顾名思义,PRE 处理器在源文件进入编译器之前对其进行处理。 C 语言有一个可以执行各种操作的预处理器。
#include - 将文件导入并展开到要编译的文件中。
#define - 在要编译的文件中更改为字符串的宏。
维基百科(我知道人们讨厌这个)对 C 预处理器有很好的概述。
没有什么可以阻止您使用其他文件(例如 m4)或编写自己的文件,这将根据某些模板表示法修改字符串或将字符串添加到文件中。
As the name implies the PRE processor does processing on source files before it goes to the compiler. The C language has a preprocessor that does various things.
#include - imports and expands a file into the file to be compiled.
#define - macros that are changed into strings in the file that is to be compiled.
Wikipedia (I know guys people hate this) has a good overview for the C preprocessor.
There is nothing that stops you from using another one such as m4 or writing your own, which would modify or add strings into file based on certain template notation.
预处理器是一个在主处理之前处理某些内容的应用程序。
在这种情况下,预处理器在编译器处理源代码以生成代码之前准备源代码。预处理器负责删除注释、扩展#defines 和#include 文件等。
所有预处理器任务背后的主要原则是,并程对编程语言的语法一无所知。它只是进行无意识的文本操作。
A preprocessor is an application which processes something before the main processing.
In this context, the preprocessor prepares the source code before the compiler works on it to generate code. The preprocessor handles removing comments, expanding #defines, and #including files, among other thiings.
The main principle behind all it preprocessor tasks, is that the PP knows nothing about the syntax of the programming language. It's just doing mindless text manipulation.
您在 C 或 C++ 等语言中遇到的预处理器基本上是一个自动文本操纵器。可以这么说,它操纵源代码。
指令是控制预处理器的命令。
例如:
#include
表示 stdio.h 应复制到当前文件中的此位置。需要注意的是,预处理器仅在文本级别上工作。它复制或插入文本,或删除文本,但它不会对汇编进行任何转换,也不会进一步进行任何链接或编译。
为了不多次复制文本,您经常会在头文件中看到此构造:
这可以确保头文件仅复制一次到预处理后要编译的完整代码中(因为您可能有多个
.c
文件,其中包含#include "headerfile.h"
。)。在 C 或 C++ 的 gcc 中使用预处理器时,所有预处理器指令均以 # 为前缀。不过,根据编译器套件和编程语言的不同,这可能会有所不同。
The preprocessors you encounter in languages like C or C++, is basically an automatic text manipulator. It manipulates the source code, so to say.
A directive is a command to control that preprocessor.
For instance:
#include <stdio.h>
means stdio.h should be copied in this place in your current file.What's important to note, is that the preprocessor only works on a textual level. It copies or inserts text, or deletes it, but it doesn't do any translation into Assembly, nor does it do any linking or compiling further down the line.
In order not to copy text multiple times over, you'd often see this construct in header files:
This ensures, the headerfile is copied only once into the complete code that is to be compiled after preprocessing (since you may have multiple
.c
files with#include "headerfile.h"
in it.).All preprocessor directives are prefixed with # when using the preprocessor in gcc for C or C++. Depending on compiler suite and programming language, this may be different, though.
C 预处理器指令的规范参考是 C 的第 6.10 节标准(链接指向 N1570,这是 2011 年 ISO C 标准的最新公开草案)。
The canonical reference for C preprocessor directives is section 6.10 of the C standard (the link is to N1570, the latest public available draft of the 2011 ISO C standard).