澄清关于在C / C ++中使用的头文件和头文件包含 [英] Clarification about the header-guards and header-file inclusion used in C/C++

查看:185
本文介绍了澄清关于在C / C ++中使用的头文件和头文件包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道人们建议在标题文件中包含标题保护,以防止标题文件内容被预处理程序多次插入源代码文件。

I know people recommend including header guards in header files, to prevent header files contents from being inserted by the pre-processor into the source-code files more than once.

但请考虑以下情况:

假设我有文件 main。 cpp stuff.cpp commonheader.h

如果 .cpp 文件尝试多次包括 commonheader.h ,然后预处理程序
将停止发生这种情况,并且在编译为目标代码后,我们得到

If either .cpp files tries to include commonheader.h more than once, then the preprocessor will stop that from happening, and after compiling to object code we get,

main.o 只包含commonheader.h 的内容。

main.o containing the contents of commonheader.h exactly once.

<$>

请注意,commonheader的内容已在文件中重复,但不在同一个 .o 文件中。

Note that the contents of commonheader, have been repeated across the files, but not within the same .o file.

那么在链接步骤中会发生什么?由于.o文件被融合到一个可执行的
中,我们必须确保第二次,不会重复commonheader的内容。编译器是否负责?如果不是,那不是一个问题,当我们处理巨大的头文件,导致跨文件的代码重复,导致大的可执行大小。

So what happens during the linking step? Since the .o files are being fused into an exectuable we will have to ensure for a second time that the contents of commonheader are not being repeated. Does the compiler take care of that? If not, wouldn't that be a problem when we are dealing with huge header files, giving rise to code repetition across files and leading to large executable sizes.

如果我在问题的任何地方犯了一些概念上的错误,请纠正我。

If I am making some conceptual mistake anywhere in the question, please correct me.

推荐答案

通常,您的头文件不应实际定义任何符号, 。所以commonheader.h看起来像这样(省略include guard):

Typically your header file should not actually define any symbols, it should just declare them. So commonheader.h would look like this (omitting the include guards):

void commonFunc1(void);
void commonFunc2(void);

在这种情况下,没有问题。如果您在 main.cpp stuff.cpp commonFunc1 >, main.o stuff.o 会知道他们想要链接一个符号 commonFunc1 ,链接器将尝试找到该符号。如果链接器没有找到符号,您会得到一个未定义的引用错误。 commonFunc1 的实际定义需要在某些cpp文件中。

In that case, there is no problem. If you call commonFunc1 in main.cpp and stuff.cpp, both main.o and stuff.o will know they want to link against a symbol called commonFunc1 and the linker will try to find that symbol. If the linker doesn't find the symbol, you get an undefined reference error. The actual definition of commonFunc1 needs to be in some cpp file.

如果你真的想定义函数头文件,使用 static ,以便链接器不会看到它们。所以你的commonheader.h可能如下:

If you really want to define functions in your header file, use static so that the linker does not see them. So your commonheader.h could look like:

static void commonFunc1()
{
    /* ... do stuff ... */
}

在这种情况下,不知道 commonFunc1 ,并且不会发生错误。这可能会增加可执行文件的大小;你可能会得到两个副本的代码 commonFunc1

In this case, the linker does not know about commonFunc1 and no errors will occur. This could increase the executable size though; you'll probably end up with two copies of the code for commonFunc1.

这篇关于澄清关于在C / C ++中使用的头文件和头文件包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆