C++:include 指令和 header 文件有什么区别?
C++: What is the difference between an include directive and a header file?
根据我的理解,include 指令是任何继承的 .cpp
文件(如果这是正确的词),header 文件是具有 .h
的文件附加到文件名。在我们的 C++ class 中它非常模糊(不断切换(自然)语言)但据我了解 headers 只是函数的声明,其他包含指令然后定义这些函数,是对吗?
任何帮助表示赞赏:)
一个"directive"指的是一个preprocessor macro,所以一个"include directive"就意味着
#include "foo.h"
头文件就是您将使用 #include
directive.
提取的实际文件
#include
是预处理器指令。预处理器指令 指示 预处理器的行为。预处理器 处理 即在编译之前修改源代码。
预处理器将 #include
指令替换为作为指令参数的文件内容。以这种方式包含的文件称为头文件,因为它们通常位于文件的开头(头部)。因此,#include <iostream>
将被名为 iostream
的头文件的内容替换(除了,iostream
等标准库头文件不一定作为文件存在)。
在指令中包含头文件允许在多个源文件的开头使用相同的头文件。但是,C++ 标准不允许跨多个源文件定义多个函数和变量(尽管有一些例外)。这就是为什么头文件通常只包含函数和变量的声明,而不包含定义。编译器必须知道函数和变量的声明,以便理解使用它们的程序。
What is the difference between an include directive and a header file?
从我对包含指令和头文件的描述看来,它们是完全不同的概念。您的问题类似于:"What is the difference between a pen, and a short story"。你可以用笔写一篇短篇小说,但它们不是很可比,因为它们是不同的概念。同样,您可以使用 include 指令包含一个头文件,但它们几乎没有可比性。
what the difference is (if there is any) between the files you include
that do and don't have a ".h" in them.
答案 - 与 'include directive' 没有区别。编译器通常会关心,但如果您正确放置 include 就不会抱怨。
也许一个例子可能会有所帮助。但首先是一个总结:
包含指令
-- 不是代码
-- 标记(在原始代码中)删除和替换 include 指令以创建编译单元的位置
-- 并标识包含替换文本的文件
替换文本可以是任何类型的文本,代码也可以。所有语法规则仍然适用,编译器永远不会看到 'include directive',只是包含指令文件的内容。
考虑到这一点,在下面的实际功能代码片段中,请注意在 class 方法属性的中间,我有一个 "include directive" 指定一个文件:
int exec_3of3() // EXPERIMENT 3 of 3
{
// copied your data from the SO post to a file named 'dumy447.inc'
// using my editor (emacs) I lightly modified the file:
// 1) inserted a quote at the beginning
// 2) removed the only line feed (after the 2500)
// 3) appended a quote to the end of the file (after 11161)
// With these 3 changes, the file can be used directly in the code!
std::stringstream ssIn (
#include "./dumy447.inc"
);
assert(ssIn.good());
// BAM! - a stream filled with your text data
...
我认为 include 指令清晰可见...这是此代码段中以“#include”开头的单行。
在此示例中,指定的文件名根本不是可执行代码,文件扩展名并不重要,但按照惯例,它不应是 .hh(或 .hpp)或 .cc(或 .cc)。丙氨酸)。 (这些也可以,但可能会让您的读者更加困惑。)
该文件是只有一行的简单文本:
" 2500 19325 74348 68955 ... 48428 83799 91649 11161 "
^ ^
starts with double quote and ends with double quote
三重点表示我隐藏了很多信息。例如,此文件包含 1 行大约 14757 个字符,全部为数字和空格。
总结:
"include directive" 几乎没有内容和地点的限制。如果你做错了,你的编译器会抱怨。
预处理代码的结果称为"compilation unit"。
"compilation unit" 具有原始文件(包含 'include directive')的所有代码,但不再包含 'include directive'。
'include directive' 已被文件内容替换,在本例中是描述一些数据的文本文件。
'include directive'不关注文件范围,无论是'.hh'还是'.cc'。它用于将文件的各个部分预处理成"compilation unit",以便提交给编译器。
根据我的理解,include 指令是任何继承的 .cpp
文件(如果这是正确的词),header 文件是具有 .h
的文件附加到文件名。在我们的 C++ class 中它非常模糊(不断切换(自然)语言)但据我了解 headers 只是函数的声明,其他包含指令然后定义这些函数,是对吗?
任何帮助表示赞赏:)
一个"directive"指的是一个preprocessor macro,所以一个"include directive"就意味着
#include "foo.h"
头文件就是您将使用 #include
directive.
#include
是预处理器指令。预处理器指令 指示 预处理器的行为。预处理器 处理 即在编译之前修改源代码。
预处理器将 #include
指令替换为作为指令参数的文件内容。以这种方式包含的文件称为头文件,因为它们通常位于文件的开头(头部)。因此,#include <iostream>
将被名为 iostream
的头文件的内容替换(除了,iostream
等标准库头文件不一定作为文件存在)。
在指令中包含头文件允许在多个源文件的开头使用相同的头文件。但是,C++ 标准不允许跨多个源文件定义多个函数和变量(尽管有一些例外)。这就是为什么头文件通常只包含函数和变量的声明,而不包含定义。编译器必须知道函数和变量的声明,以便理解使用它们的程序。
What is the difference between an include directive and a header file?
从我对包含指令和头文件的描述看来,它们是完全不同的概念。您的问题类似于:"What is the difference between a pen, and a short story"。你可以用笔写一篇短篇小说,但它们不是很可比,因为它们是不同的概念。同样,您可以使用 include 指令包含一个头文件,但它们几乎没有可比性。
what the difference is (if there is any) between the files you include that do and don't have a ".h" in them.
答案 - 与 'include directive' 没有区别。编译器通常会关心,但如果您正确放置 include 就不会抱怨。
也许一个例子可能会有所帮助。但首先是一个总结:
包含指令
-- 不是代码
-- 标记(在原始代码中)删除和替换 include 指令以创建编译单元的位置
-- 并标识包含替换文本的文件
替换文本可以是任何类型的文本,代码也可以。所有语法规则仍然适用,编译器永远不会看到 'include directive',只是包含指令文件的内容。
考虑到这一点,在下面的实际功能代码片段中,请注意在 class 方法属性的中间,我有一个 "include directive" 指定一个文件:
int exec_3of3() // EXPERIMENT 3 of 3
{
// copied your data from the SO post to a file named 'dumy447.inc'
// using my editor (emacs) I lightly modified the file:
// 1) inserted a quote at the beginning
// 2) removed the only line feed (after the 2500)
// 3) appended a quote to the end of the file (after 11161)
// With these 3 changes, the file can be used directly in the code!
std::stringstream ssIn (
#include "./dumy447.inc"
);
assert(ssIn.good());
// BAM! - a stream filled with your text data
...
我认为 include 指令清晰可见...这是此代码段中以“#include”开头的单行。
在此示例中,指定的文件名根本不是可执行代码,文件扩展名并不重要,但按照惯例,它不应是 .hh(或 .hpp)或 .cc(或 .cc)。丙氨酸)。 (这些也可以,但可能会让您的读者更加困惑。)
该文件是只有一行的简单文本:
" 2500 19325 74348 68955 ... 48428 83799 91649 11161 "
^ ^
starts with double quote and ends with double quote
三重点表示我隐藏了很多信息。例如,此文件包含 1 行大约 14757 个字符,全部为数字和空格。
总结:
"include directive" 几乎没有内容和地点的限制。如果你做错了,你的编译器会抱怨。
预处理代码的结果称为"compilation unit"。
"compilation unit" 具有原始文件(包含 'include directive')的所有代码,但不再包含 'include directive'。
'include directive' 已被文件内容替换,在本例中是描述一些数据的文本文件。
'include directive'不关注文件范围,无论是'.hh'还是'.cc'。它用于将文件的各个部分预处理成"compilation unit",以便提交给编译器。