C ++头文件问题 [英] C++ header file question

查看:137
本文介绍了C ++头文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用类时尝试了一些c ++代码,这个问题发生在我身上,这让我有些烦恼。

I was trying out some c++ code while working with classes and this question occurred to me and it's bugging me a little.

我创建了一个包含我的类定义的头文件和一个包含实现的cpp文件。

I have created a header file that contains my class definition and a cpp file that contains the implementation.

如果我在不同的cpp文件中使用这个类,为什么我包含头文件而不是包含类实现的cpp文件?

If I use this class in a different cpp file, why am I including the header file instead of the cpp file that contains the class implementations?

如果我包括类实现文件,那么类头文件应该自动导入(因为我已经包括头文件在实现文件)?这不是更自然吗?

If I include the class implementation file, then the class header file should be imported automatically right (since i've already included the header file in the implementation file)? Isn't this more natural?

对不起,如果这是一个愚蠢的问题,我真正感兴趣的知道为什么大多数人包括.h而不是.cpp文件,当后者似乎更自然(我知道python有点,也许这就是为什么它似乎自然对我至少)。

Sorry if this is a dumb question, i'm genuinely interested in knowing why most people include .h instead of .cpp files when the latter seems more natural (I know python somewhat, maybe that's why it seems natural to me atleast). Is it just historical or is there a technical reason concerning program organisation or maybe something else?

推荐答案

因为当你编译另一个文件,C ++实际上不需要知道实现。它只需要知道每个函数的签名(它需要的参数和返回的内容),每个类的名称, #define d和其他摘要信息,以便它可以检查您正在使用函数和类。在链接器运行之前,不同的 .cpp 文件的内容不会组合在一起。

Because when you're compiling another file, C++ doesn't actually need to know about the implementation. It only needs to know the signature of each function (which paramters it takes and what it returns), the name of each class, what macros are #defined, and other "summary" information like that, so that it can check that you're using functions and classes correctly. The contents of different .cpp files don't get put together until the linker runs.

您有 foo.h

int foo(int a, float b);

foo.cpp

#include "foo.h"
int foo(int a, float b) { /* implementation */ }

bar.cpp

#include "foo.h"
int bar(void) {
    int c = foo(1, 2.1);
}

当您编译 foo.cpp ,它变成 foo.o ,当你编译 bar.cpp 时,它变成 bar.o 。现在,在编译过程中,编译器需要检查 foo() foo.cpp 同意 bar.cpp 中函数 foo()的使用(即接受 int float 并返回 int )。它的方式是通过在 .cpp 文件中包含相同的头文件,并且如果定义和用法都与标题中的声明一致,

When you compile foo.cpp, it becomes foo.o, and when you compile bar.cpp, it becomes bar.o. Now, in the process of compiling, the compiler needs to check that the definition of function foo() in foo.cpp agrees with the usage of function foo() in bar.cpp (i.e. takes an int and a float and returns an int). The way it does that is by making you include the same header file in both .cpp files, and if both the definition and the usage agree with the declaration in the header, then they must agree with each other.

但是编译器实际上并不包括 foo()的实现 bar.o 。它只包括一个汇编语言指令调用foo 。所以当它创建 bar.o 时,它不需要知道任何关于 foo.cpp 的内容。然而,当你进入链接阶段(编译后发生),链接器实际上需要知道 foo()的实现,因为它将包括在最终程序中实现,并用调用0x109d9829 (或者它决定的内存地址)替换调用foo 函数 foo()应为)。

But the compiler doesn't actually include the implementation of foo() in bar.o. It just includes an assembly language instruction to call foo. So when it creates bar.o, it doesn't need to know anything about the contents of foo.cpp. However, when you get to the linking stage (which happens after compilation), the linker actually does need to know about the implementation of foo(), because it's going to include that implementation in the final program and replace the call foo instruction with a call 0x109d9829 (or whatever it decides the memory address of function foo() should be).

请注意,链接器实现 foo()(在 foo.o )同意使用 foo()(在 bar.o ) - 例如,它不检查 foo code>正在调用 int float 参数!在汇编语言中进行这种检查是非常困难的(至少比检查C ++源代码更难),因此链接器依赖于知道编译器已经检查过了。这就是为什么你需要头文件,以提供信息给编译器。

Note that the linker does not check that the implementation of foo() (in foo.o) agrees with the use of foo() (in bar.o) - for example, it doesn't check that foo() is getting called with an int and a float parameter! It's kind of hard to do that sort of check in assembly language (at least, harder than it is to check the C++ source code), so the linker relies on knowing that the compiler has already checked that. And that's why you need the header file, to provide that information to the compiler.

这篇关于C ++头文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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