In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you what you should pay attention to when dealing with C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
What exactly does such code mean? First of all, _ _ cplusplus is a custom macro in cpp, so defining this macro means that this is a piece of cpp code, that is, the above code means: if this is a piece of cpp code, then add extern "C" {and} to deal with the code.
# ifdef _ cplusplus extern "C" {# endif / / A code # ifdef _ cplusplus} # endif
To understand why you use extern "C++", you have to start with the overloaded handling of functions in cpp. In C++, in order to support the overloading mechanism, the name of the function should be processed in the compiled assembly code, such as the return type of the function and so on. In C++, it is just a simple function name and no other information is added. In other words: C++ and C on the resulting function name processing is not the same.
For example, in the following simple function, let's take a look at the changes in the assembly code with and without extern "C++":
Int f (void) {return 1;}
The assembly code generated when adding extern "C" is:
.file "test.cxx" .text .align 2 .globl _ f .def _ f; .scl 2; .type 32; .endef _ f: pushl% ebp movl% esp,% ebp movl $1,% eax popl% ebp ret
Two pieces of assembly code are also generated using the gcc-S command, all are the same, except for the generated function name, one is _ f, the other is _ _ Z1fv. Now that we understand the impact on function names with and without extern "C", let's continue our discussion: why do we need to use extern "C"?
When the father of C++ designed C++, considering that a large amount of C++ code already existed at that time, in order to support the original C++ code and the C++ library, we need to support it as much as possible in C++, and extern "C++" is one of the strategies. Imagine a library file that has been written in C++ and works well.
At this point we need to use the library file, but we need to use C++ to write the new code. If this code uses C++ to link the C library file, then a link error will occur. Let's look at a piece of code.
Understand the impact on function names after joining and not joining extern "C++", let's continue our discussion: why do we need to use extern "C++"? the father of C++ in the design of C++, taking into account that there is already a lot of C code, in order to support the original C++ code and has written C++ library, need to support C++ as much as possible in order to support the C++, and extern "C++" is one of the strategies.
Imagine this situation: a library file has been written in C++ and works well, we need to use this library file, but we need to use C++ to write this new code. If this code uses C++ to link the C++ library file, then a link error will occur. Let's look at a piece of code: first, we use C processing to write a function.
These are the matters needing attention when dealing with C++ shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.