简体   繁体   English

为什么我不需要在头文件中包含library.cpp?

[英]Why don't I need to include library.cpp in the header?

I have a question about libraries. 我对图书馆有疑问。 When I write a library I have 2 files: library.h and library.cpp . 当我写一个库时,我有两个文件: library.hlibrary.cpp

The first one contains the prototypes of the functions and the second one contains the definitions. 第一个包含函数的原型,第二个包含定义。 Well, in library.cpp I include #include "library.h" , so they are connected in one way, but what happens with the header? 好吧,在library.cpp我包含#include "library.h" ,所以它们以一种方式连接,但是标题会发生什么? Why don't I have to write #include "library.cpp" in the header? 为什么我不必在标题中写#include "library.cpp"

When I use the library in the main file, I write #include "library.h" , which includes the prototypes, but how does the compiler know where the definitions are? 当我在主文件中使用库时,我写了#include "library.h" ,其中包含原型,但编译器如何知道定义的位置?

Explained briefly: 简要解释:

(1) Your library.cpp file is sent to the preprocessor. (1)您的library.cpp文件被发送到预处理器。

(2) The preprocessor reads the line #include "library.h" and goes and finds the file library.h . (2)预处理器读取#include "library.h"行并找到文件library.h

(3) The contents of library.h are literally copied into the library.cpp file. (3)将library.h的内容字面上复制到library.cpp文件中。

(4) The library.cpp file is compiled and linked with the main file. (4) library.cpp文件被编译并与主文件链接。

Thus, all of the "prototypes" in the header are copied into the top of the implementation file. 因此,标题中的所有“原型”都被复制到实现文件的顶部。 .cpp files are compiled and linked. .cpp文件已编译和链接。 The header files themselves are not compiled -- their contents are copied into the .cpp files. 头文件本身不会被编译 - 它们的内容被复制到.cpp文件中。

The preprocessor pulls the header file into the CPP file, so the compiler sees the prototypes and the definitions together. 预处理器将头文件拉入CPP文件,因此编译器一起查看原型和定义。

If you were to pull the CPP file into the header file, you would either send the preprocessor into an infinite loop, or by using 如果要将CPP文件拉入头文件,则可以将预处理器发送到无限循环,或者使用

#ifndef __FOOBAR__  
#define __FOOBAR__  
(code file)  
#endif

around the header AND source files you would read the file just once. 在标题和源文件周围,您只需读取一次文件。

There is a tool called linker which is responsible for link your generated object files. 有一个名为linker的工具,负责链接生成的目标文件。 You should look for compilation process to understand it better. 您应该寻找编译过程以更好地理解它。

All the cpp files are compiled seperately and the code is accumulated in a big pile somewhere. 所有cpp文件都是单独编译的,代码在某个地方堆积起来。 The linker collects all the symbols in your code and assigns them an address within this pile. 链接器收集代码中的所有符号,并为它们分配一个地址。 So although your file doesn't see library.cpp directly, it knows the symbols from library.h and the assigned addresses. 因此,尽管您的文件没有直接看到library.cpp,但它知道library.h中的符号和分配的地址。 It can then zoom directly to the required code in the big pile. 然后它可以直接缩放到大堆中所需的代码。 Not the most technical answer I know.. 不是我知道的最技术性的答案..

It was my understanding that when you type #include "library.h" in your code the compiler is set to also load library.cpp. 我的理解是,当您在代码中键入#include "library.h" ,编译器也会设置为加载library.cpp。 I was taught in school that there isn't really any way to break this connection unless you were to manually compile and link each file either using compiler commands or a makefile appropriate for the system you're using (Windows, *nix, MacOS, etc.) 我在学校教过,除非你使用编译器命令或适合你正在使用的系统的makefile手动编译和链接每个文件,否则没有任何方法可以打破这种连接(Windows,* nix,MacOS,等等。)

You have to type #include "library.h" in the library.cpp file because that is where the function prototypes are stored. 您必须在library.cpp文件中键入#include "library.h" ,因为这是存储函数原型的位置。 You can "cheat" and put the function prototypes in the .cpp file and just type #include "library.cpp" but this is bad practice and is generally discouraged in the programming community. 您可以“欺骗”并将函数原型放在.cpp文件中,只需键入#include "library.cpp"但这是不好的做法,通常不鼓励编程社区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM