预处理器指令
当我们看到#include
时,它被认为是一个预处理器指令。
#include
--->并且
,我认为:
--->预处理器
但是,“预处理器”和“指令”是什么意思?
When we see #include <iostream>
, it is said to be a preprocessor directive.
#include
---> directive
And, I think:
<iostream>
---> preprocessor
But, what is meant by "preprocessor" and "directive"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
思考“指令”和“给予指示”(即命令)之间的关系可能会有所帮助。 “预处理器指令”是对预处理器的指示,说明在编译的后期阶段开始之前应该对代码进行的更改。
但是,预处理器是什么?嗯,它的名字反映了它在编译的“主要”阶段之前处理源代码。它只是处理文本源代码,以各种方式修改它。预处理器甚至不理解它所操作的标记 - 它没有类型或变量、类或函数的概念 - 它只是用引号和/或括号分组、逗号和/或空格分隔的文本来进行处理。这个额外的过程为选择、组合甚至生成程序的各个部分提供了更大的灵活性。
编辑@SWEngineer 的评论:许多人发现将预处理器视为修改 C++ 程序的单独程序,然后将其输出提供给“真正的”C++ 编译器(这几乎是过去的方式)是有帮助的。当预处理器看到
#include
时,它会认为“啊哈哈 - 这是我所理解的,我会处理这个问题,而不是盲目地将它传递给 C++编译器”。因此,它会搜索许多目录(一些标准目录,例如/usr/include
以及编译器安装其自己的标头的位置,以及在目录上使用-I
指定的其他目录)命令行)寻找名为“iostream”的文件。当它找到它时,它会将输入程序中的“#include”行替换为名为“iostream”的文件的完整内容,并将结果添加到输出中。但是,它然后移动到从“iostream”文件读取的第一行,寻找它理解的更多指令。所以,预处理器非常简单。它可以理解
#include
、#define
、#if
/#elif
/#endif、
#ifdef
和$ifndef
、#warning
和#error
,但仅此而已。它不知道“int
”是什么、模板、类或任何“真正的”C++ 东西。它更像是一些自动编辑器,可以剪切和粘贴部分文件和代码,准备 C++ 编译器最终将看到和处理的程序。预处理器仍然非常有用,因为它知道如何在所有这些不同的目录中查找程序的各个部分(编译的下一阶段不需要知道任何相关信息),并且它可以删除可能适用于其他目录的代码计算机系统,但在正在使用的系统上无效。它还可以允许程序使用简短的宏语句,生成大量真正的C++代码,使程序更易于管理。It may help to think of the relationship between a "directive" and being "given directions" (i.e. orders). "preprocessor directives" are directions to the preprocessor about changes it should make to the code before the later stages of compilation kick in.
But, what's the preprocessor? Well, its name reflects that it processes the source code before the "main" stages of compilation. It's simply there to process the textual source code, modifying it in various ways. The preprocessor doesn't even understand the tokens it operates on - it has no notion of types or variables, classes or functions - it's all just quoted- and/or parentheses- grouped, comma- and/or whitespace separated text to be manhandled. This extra process gives more flexibility in selecting, combining and even generating parts of the program.
EDIT addressing @SWEngineer's comment: Many people find it helpful to think of the preprocessor as a separate program that modifies the C++ program, then gives its output to the "real" C++ compiler (this is pretty much the way it used to be). When the preprocessor sees
#include <iostream>
it thinks "ahhha - this is something I understand, I'm going to take care of this and not just pass it through blindly to the C++ compiler". So, it searches a number of directories (some standard ones like/usr/include
and wherever the compiler installed its own headers, as well as others specified using-I
on the command line) looking for a file called "iostream". When it finds it, it then replaces the line in the input program saying "#include " with the complete contents of the file called "iostream", adding the result to the output. BUT, it then moves to the first line it read from the "iostream" file, looking for more directives that it understands.So, the preprocessor is very simple. It can understand
#include
,#define
,#if
/#elif
/#endif
,#ifdef
and$ifndef
,#warning
and#error
, but not much else. It doesn't have a clue what an "int
" is, a template, a class, or any of that "real" C++ stuff. It's more like some automated editor that cuts and pastes parts of files and code around, preparing the program that the C++ compiler proper will eventually see and process. The preprocessor is still very useful, because it knows how to find parts of the program in all those different directories (the next stage in compilation doesn't need to know anything about that), and it can remove code that might work on some other computer system but wouldn't be valid on the one in use. It can also allow the program to use short, concise macro statements that generate a lot of real C++ code, making the program more manageable.#include
是预处理器指令,
只是除此指令之外提供的参数,在本例中,它恰好是一个文件名。有些预处理器指令接受参数,有些则不接受,例如,
共同的特点是它们都以
#
开头。在旧时代,预处理器是一个单独的工具,它在将源代码传递到编译器前端之前对其进行预处理,执行宏替换并包含头文件等。现在,预处理器通常是编译器的一个组成部分,但它本质上只是做同样的工作。
#include
is the preprocessor directive,<iostream>
is just an argument supplied in addition to this directive, which in this case happens to be a file name.Some preprocessor directives take arguments, some don't, e.g.
The common feature is that they all start with
#
.In Olden Times the preprocessor was a separate tool which pre-processed source code before passing it to the compiler front-end, performing macro substitutions and including header files, etc. These days the pre-processor is usually an integral part of the compiler, but it essentially just does the same job.
预处理器指令,例如#define 和#ifdef,通常用于使源程序在不同的执行环境中易于更改和编译。源文件中的指令告诉预处理器执行特定操作。例如,预处理器可以替换文本中的标记,将其他文件的内容插入到源文件中...
#include
是一个预处理器指令,意味着它由预处理器使用< /em> 编译器的一部分。这发生在编译过程“之前”。#include
需要指定要包含的“内容”,这由参数iostream
提供。这告诉预处理器包含文件 iostream.h。详细信息:
Preprocessor directives, such as
#define
and#ifdef
, are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file...#include
is a preprocessor directive meaning that it is use by the preprocessor part of the compiler. This happens 'before' the compilation process. The#include
needs to specify 'what' to include, this is supplied by the argumentiostream
. This tells the preprocessor to include the fileiostream.h
.More information: