Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of extern "C"

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the usage of extern "C"? in view of this question, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

The C++ language was originally created as "abetterC", but this does not mean that global variables and functions similar to C in C++ are compiled and concatenated in exactly the same way as C. As a language that wants to be compatible with C, C++ retains some of the features of the procedural language (known as "incomplete object orientation"), so it can define global variables and functions that do not belong to any class. However, C++ is an object-oriented programming language after all. In order to support the overloading of functions, the way C++ handles global functions is obviously different from that of C.

Deep revelation of extern "C"

Extern "C" has a dual meaning, which can be obtained literally: first, the target modified by it is "extern"; second, the target modified by it is "C". Let's interpret these two meanings in detail.

Functions or variables qualified by extern "C" are of type extern

Extern is a keyword that indicates the scope (visibility) of functions and global variables in the Cracket + language. This keyword tells the compiler that its declared functions and variables can be used in this module or other modules. It is just a declaration of a variable, which is not defining the variable an and does not allocate memory space for a. The variable a can only be defined once as a global variable in all modules, otherwise a connection error will occur.

Typically, the functions and global variables that this module provides to other modules are declared with the keyword extern in the module's header file. For example, if module B wants to reference the global variables and functions defined in module A, you only need to include the header file of module A. In this way, when a function in module An is called in module B, during the compilation phase, module B cannot find the function, but it will not report an error; it will find this function in the object code generated by module A compilation during the connection phase.

The keyword corresponding to extern is static, and the global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module, it can not be modified by extern "C".

As an object-oriented language, C++ supports function overloading, while the procedural language C does not. The name of the function compiled by C++ in the symbol library is different from that of the C language. For example, suppose the prototype of a function is:

Voidfoo (intx,inty)

The function is compiled by the C compiler with the name _ foo in the symbol library, while the C++ compiler generates names like _ foo_int_int (different compilers may generate different names, but all use the same mechanism, and the new name is "mangledname").

Names like _ foo_int_int contain function name, number of function parameters and type information. C++ relies on this mechanism to achieve function overloading. For example, in C++, the symbols generated by the compilation of the function voidfoo (intx,inty) and voidfoo (intx,floaty) are different, followed by _ foo_int_float.

Similarly, variables in C++ support class member variables and global variables in addition to local variables. The class member variable of the program written by the user may have the same name as the global variable. To tell the difference. In essence, when compiling, the compiler is similar to the function and gives a unique name to the variable in the class, which is different from the global variable name of the same name in the user program.

This is the answer to the question about what is the usage of extern "C". I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report