[英]Include header files in c++
我要问的C ++ Premier话不多,这就是我在Google上搜索LINK的意思 :
当编译器编译#include“ example.h”行时,它将example.h的内容复制到当前文件中。
因此,如果是这样,在下面的示例中,为什么Bh不了解Ah? 文件是如何编译的? 我是否必须在使用它的每个文件中都包含Ah,然后在使用这些文件的program.h中包含每个使用Ah的文件?
In program.h
#include "A.h"
#include "B.h"
警告:非常错误的代码:
ah
#ifndef A_H
#define A_H
#define SOME_LIT "string lit in A.h"
#endif
bh
#ifndef B_H
#define B_H
#include <iostream>
void foo() { std::cout << SOME_LIT << '\n'; }
#endif
main.cpp
#include "a.h"
#include "b.h"
int main()
{
foo();
}
打印:
$ ./a.out
string lit in A.h
因此,您可以看到bh
了解ah
的define
。 如果您忘记了#include "ah"
或将其放在 #include "bh"
下面 ,则可能会中断。
但是,作为一般规则,您应该在需要的任何文件中明确#include
一个标头。 这样,您就知道只关心main
foo
,因此只需#include
foo
标头,即bh
:
#include "b.h"
int main()
{
foo();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.