c/c++ 文件包含宏的问题(#include)
怪我咯
怪我咯 2017-04-17 13:58:38
0
3
441

c++在新建一个类时,会新建一个a.h文件和a.cpp文件,a.cpp的第一句是#include “a.h”。
那么我想问:
1.我在另外一个类,类b中使用类a的时候,只需包含它的头文件#include “a.h”,那这时是不是只把a.h中的类声明代码包含在了类b文件中,还是说把类a的声明代码和实现代码都包含了?
2.不管是哪种包含,预处理程序会把相应代码插入到对应的文件中,在编译时,编译器是把每一个cpp文件分开作为一个单元来编译,还是直接对一个文件里面所有的类进行编译

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
刘奇

1: Only the class declaration code in a.h is included in the class b file
After the program is compiled, all the compiled files will be linked into an executable file, which is equivalent to summarizing all the codes
So you only need to include the declaration
2: Each cpp is compiled separately
If there is a connection between different cpps, such as a.cpp calling a function in b.cpp, after b.cpp changes, only b.cpp will be compiled cpp

that is related to b.cpp
洪涛
  1. contains the content in a.h. If there is an implementation in a.h, then there is an implementation.

  2. The specific action of compilation takes Linux as an example. You can use cc -E a.cpp to see the results after preprocessing. Each .cpp file will be regarded as a compilation unit and compiled to generate a .o file.

伊谢尔伦

1: When including the header file .h, only the code in the header file will be included, and the corresponding .cpp will not be included.
So generally only the declaration of variables or functions is provided in .h, but no implementation code is provided. Otherwise, when a header file is included by multiple .cpp, the variable or function will be reported to be repeatedly defined during linking;
2: Each cpp will be compiled separately into the corresponding .o file, and then linked together to form a Execution file or library;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template