包含防护是否意味着只有一个 .cpp 文件获取标头内容?
当我包含一个头文件时,比如说,
//myheader.h
#ifndef MY_HEADER_H
#define MY_HEADER_H
//....
#endif
我
//mycpp1.cpp
#include "myheader.h"
被告知的是,当 mycpp1.cpp 包含 myheader.h 时,MY_HEADER_H
被定义,因此任何再次包含它的尝试都将导致 false。
现在,如果我想将它包含在 mycpp2.cpp 中。
//mpcpp2.cpp
#include "myheader.h"
它会被包含吗?或者它是否使用与第一次包含时相同的声明?
When I include a header file, lets say,
//myheader.h
#ifndef MY_HEADER_H
#define MY_HEADER_H
//....
#endif
into,
//mycpp1.cpp
#include "myheader.h"
What I'm told is, when mycpp1.cpp includes myheader.h, MY_HEADER_H
gets defined so any attempt to include it again will result false.
Now, if i want to include it in mycpp2.cpp.
//mpcpp2.cpp
#include "myheader.h"
Would it get included or does it use the same declarations when it was included the first time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Preprocessor definitions are seperate for each file. So if you
#include
myheader.h into two seperate .cpp files, it will be included twice, not once. One per .cpp.continue
Of course there are complex cases, but here is the point:
Try to look at .cpp (source) files as distinct entities.
If you don't make really weird things, then
if you remove all .cpp files except the one you bother,
you can stil compile, because in the compiling stage no need
for definitions, you care only the name (declaration) of things.
So, compiling N source files in one run is essentially this:
where every line is a distinct compilation unit, which
renders the SourceX.CPP and the included headers into an Object file.
So we got here N separate thing.
This way, if you don't change the common headers, then you don't have to
recompile unmodified source files. Of course, if you modify a source file,
you have to recompile that. And finally, if you modify a common header, then you have
to recompile every source file that included it.
Here I have to mention, that before the compiling phase, all
#inlude "filename.ext"
line (preprocessor directive) will be replacedwith the exact content of the filename.ext file, whatever it is.
Then linking is a different problem, in that stage
the goal is to create one single file from that N object files. (I repeat, this is the simple case)
This is linking:
Imagine the affected the object files as a bag of values and
algorithms (function definitions). For example, somewhere in the bag there must be exactly one
main
function (definition), so the linker will definitely know whatto do when you execute the program.
Hope you got it
Guess what happens, if you write a global function's definition into a header file,
and then you include it in two separate compilation unit, then you try to link them.
The Answer: linker error - multiple definitions, since you can compile them separately.
continue
If MY_HEADER_H is just defined in mycpp1.cpp, the header file will be included in mycpp2.cpp
what the trick is really for is like:
continue
As the other have stated, the header file is included once per .cpp file. But I also wanted to mention that the whole purpose of this:
is to prevent this from causing a problem
mycpp1.cpp
another thing I had trouble with when I started was when you include your header file in more than one .cpp file and have global variables defined in it... here's what I do to solve that...
myheader.h
mycpp1.cpp
mycpp2.cpp
both cpp files will print
282
continue
It will be included only once. The directive MY_HEADER_H would get defined on the first inclusion and all subsequent attempts to
#include
myheader.h would have no effect.The preprocessor directives prevail across files. Otherwise, for example, every class declared in myheader.h, would be redefined.