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--
This article mainly explains "how to understand C++ internal and external links", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to understand C++ internal and external links"!
First of all, what is a compilation unit?
We know that when compiling the code, the compiler will only compile the source file in .cpp format, and the precompiler will recursively "copy" all the # include header files of .cpp into the .cpp file, and then compile the file to generate a binary .obj file. So in fact, every .cpp file is a compilation unit.
Declaration and definition
A declaration introduces a name into a scope, and C++ can declare repeatedly in the same scope, except for the declaration of member functions and member variables in the class. The following are all statements:
Extern int number; / / external reference declaration Typedef int int32; / / typedef declares Class A; / / pre-declaration of the class Using std::cin; / / namespace reference declaration Friend f; / / friend declaration Int testFun (); / / function pre-declaration
The definition determines the unique description of an entity in a scope, and an entity cannot be defined repeatedly in the same scope. The following are definitions:
Int aittClass Myclass {… {first, second,third}; Const int m = 2 Myclass ma;Static int Void Enum () {… }
What is an internal link?
If a name is local to his compilation unit and does not conflict with the same name in other compilation units when linking, then the name has an internal link. If this entity has an internal link, it will not conflict with other entities with the same name in the .cpp file. In other words, those compilation units (.cpp) cannot show other compilation units (.cpp) functions and variables that provide their definitions.
So which entities have internal links?
1. Static (static) global variable, free function, friend function definition
two。 Definition of class
3. Inline function definition
4. Definition of Union Commons
5. Const constant definition of namespace
6. Enumerated type definition
7. All statements (some people boil it down to no link)
What is an external link?
In a multi-file program, an entity can interact with other compilation units when linking, so the entity has external links.
In other words, those compilation units (.cpp) can provide their definitions to other compilation units (.cpp), so that functions and variables used by other compilation units (.cpp) have external links.
So which entities have external links?
1. Definition of non-inline functions of a class (including member functions and static class member functions)
two。 Definition of static member variables of a class
3. Definition of namespace or global non-static free functions, non-static variables, non-friend functions
So to sum up, what's the point of defining such internal and external links?
The so-called link, because of the continuous expansion of the project, written in a .cpp file is not only difficult to maintain, but also difficult to cooperate in development. So divide the code into more organized files so that it can be compiled independently and run together in the end, that is, through links to find the code you need. At this point, you need to locate the external link to the appropriate code.
For example, the global functions and variables we define can be used by cross-module links.
There are some name definitions that represent entities that have external links, which means that they can link code across compilation units. Therefore, if an entity with an external link is declared in the header file and contained by multiple .cpp files, there may be a link conflict error, because each .cpp that contains the entity with an external link will allocate space. when multiple compilation units are linked, the connector will face multiple identical names and cannot properly link to the correct object.
Here's an example: (in VS2012 environment)
/ / lesson.hnamespace lesson {int test;} / / lesson.cpp#include "stdafx.h" # include "lesson.h" int _ tmain (intargc,_TCHAR*argv []) {system ("pause"); return0;} / / test.cpp#include "lesson.h"
We'll see.
Error LNK2005: "intlesson::test" (? test@lesson@@3HA) has been defined in lesson.obj C:\ Users\ user\ Documents\ Visual Studio 2012\ Projects\ lesson\ lesson\ stdafx.obj
Such an error prompt.
This is not the case for an entity that has an internal link because it does not conflict with other .cpp entities of the same name. For example, let's change the above lesson.h to
/ / lesson.hclass lesson {int test;}
In this way, there will be no errors, because the definition of the class has internal links.
If you define static variables, enumerate classes, make various declarations, etc., in lesson.h, these entities are still legal because of internal links, and the compiler will think that you want to have a private copy in each compilation unit.
Well, a further summary of these contents is a sentence.
There can be multiple declarations within the same scope, but can only be defined once.
Regardless of internal or external links, we all know that it is impossible to define two identical names in one {}. We know about a single .cpp file, but for multiple files, it seems to be a little dizzy. In fact, this is a truth, our external link is to let each .cpp file can be linked together, so before the .cpp file meets the first {}, their scope can be understood as the same, so the entities with external links (global functions, variables, etc.) appear before the first {}, and the name is the same, that is, there is a definition duplicate error.
Let's see that all declarations have internal links, but he can actually link to other files, because his definition is in other compilation units, so it is reasonable for multiple compilation units to have the same declaration. However, we know that there must be only one definition for this declaration.
Finally, a C++ programming suggestion is given, and carefully consider defining linked entities in the header file.
First, if the header file is a definition such as int axiom 1;, a link error will be reported when it is included in multiple .cpp files.
Second, if you want static int a = 2; such a definition will generate a copy of all the .cpp files that contain it. If it is include by a large number of source files, it will take up a lot of space and cause a waste of memory.
At this point, I believe you have a deeper understanding of "how to understand C++ internal and external links". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.