包含防护是否意味着只有一个 .cpp 文件获取标头内容?

发布于 2024-12-19 16:24:37 字数 416 浏览 1 评论 0原文

当我包含一个头文件时,比如说,

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万劫不复 2024-12-26 16:24:37

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.

初吻给了烟 2024-12-26 16:24:37

continue

Preprocessor defines are local to the current compilation unit.

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:

[ *.H + SOURCE1.CPP ] --> SOURCE1.O
[ *.H + SOURCE2.CPP ] --> SOURCE2.O
...
[ *.H + SOURCEN.CPP ] --> SOURCEN.O

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 replaced
with 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:

[ SOURCE1.O + SOURCE2.O + ... + SOURCEN.O ]  --> EXECUTABLE.FILE

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 what
to 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.

瘫痪情歌 2024-12-26 16:24:37

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:

header1.h includes header2.h.
So when you include both header1.h and header2.h in your mycpp.cpp,
header2.h will be included twice if you did not do the trick.

昵称有卵用 2024-12-26 16:24:37

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:

#ifndef MY_HEADER_H
#define MY_HEADER_H
// ..
#end if

is to prevent this from causing a problem

mycpp1.cpp

#include "myheader.h"
#include "myheader.h"
// ...

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

#ifdef MY_HEADER_H
#define MY_HEADER_H

#ifdef GLOBAL_EXTERN
extern int nGlobalInt;
#else
int nGlobalInt = 282;
#endif

#endif

mycpp1.cpp

#include "myheader.h"
// ...

cout << nGlobalInt;

mycpp2.cpp

#define GLOBAL_EXTERN
#include "myheader.h"
// ...

cout << nGlobalInt;

both cpp files will print 282

凶凌 2024-12-26 16:24:37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文