In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to talk to you about how C++ compiles the header file, which many people may not know very well. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
The left and right examples we've done before are done in a single cpp file, and when we're working on a relatively complex or large project, we obviously can't write all the code in one cpp. This requires us to split the code, but the code is logically divided and written into different cpp files.
When we compile, we can compile these cpp files separately and finally connect them together. The advantage of this is that when we modify only one file, we can compile that file separately without affecting the compilation results of other files. Generally speaking, large projects will use automated compilation tools, such as make, and will not perform the compilation process manually, but you still need to know some of the details.
Let's take a look at an example provided in C++ primer.
Now if we want to realize the function of transforming direct coordinates into polar coordinates, we need to define two structures to represent Cartesian coordinates and polar coordinates respectively, and we also need to realize the transformation from direct coordinates to polar coordinates.
Obviously, this part of the code is independent of the main program, so we can put it in a separate cpp file. The first thing to be clear is that the main () function uses the same structure as other functions, so both cpp files need to contain a declaration for that structure. Obviously copying the code is a bad choice, and it's better to write the declaration of the structure in the header file and introduce it through the # include statement.
In this way, the overall code is divided into three parts:
Header file: contains structure declaration, function declaration
Coordin.cpp: contains code related to coordinate system conversion
Main.cpp: main program
We will often use such code structures in later object-oriented chapters.
There are strict restrictions on the contents of the header file, because the header file may be introduced by multiple cpp files, so we cannot put the implementation of the function or the definition of parameters in the header file. Because multiple definitions of the same function in the same program will cause an error, and the parameters are the same.
Only the following can be written to the header file:
Function prototype (function declaration)
# symbolic constants defined by define or const
Structure declaration
Class declaration
Template declaration
Inline function (inline)
Only one header file can be introduced once in the same file, but sometimes it may be introduced repeatedly because of reference dependencies. For example, An and B header files are introduced, and An is introduced into B header files, resulting in A being introduced twice.
To solve this problem, we can add the precompiled instruction # ifndef, which means if not defined, to determine whether a definition exists. The statement between # ifndef and # endif only occurs if the definition does not exist:
# ifndef COORDIN_H_// statements#endif
In general, we use # define to create symbolic constants:
# define MAXI 4096
But since we are only here to distinguish whether to introduce or not, all we need is the name:
# ifndef COORDIN_H#define COORDIN_H// todo#endif
In this way, when introduced once, the COORDIN_H is defined, and the code will not be executed next time.
Finally, we write the complete header file code:
#
Ifndef COORDIN_H__#define COORDIN_H__struct polar {double distance, angle;}; struct rect {double x, y;}; polar rect_to_polar (rect xpros); void show_polar (polar dapos); # endif after reading the above, do you have any further understanding of how C++ writes the header file? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.