Simple parsing of extern & quot; C & quot;

Source: Internet
Author: User

Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C.
First, let's take a look.:
A. in a file, if an external variable is not defined at the beginning of the file, its valid range is limited to the end of the definition. If you need to reference this variable before the definition, you need to use the keyword "extern" to make an "external variable" for the variable before the reference, indicating that the variable is an external variable that has been defined. With this declaration, You can reasonably use the variable from the Declaration. "Extern" plays a role in the extended scope.

#include <stdio.h> A;    printf( A=;

B. in a multi-file program, if multiple files use the same external variable, one external variable cannot be defined in each file. Otherwise, the "repeated definition" error may occur. The correct method is to define external variables in any file. Other files use "extern" to declare external variables ". During compilation and connection, the system will know that the variable is an external variable defined elsewhere, so that the external variable can be used legally in this file.

#include <stdio.h> A = ; A; A*

. Extern indicates that variables or functions are defined in other source files, instead of using the include header file to reference the function, the linker searches for the variable or function in each module for the final link.
C. External Functions
When defining a function, if the keyword extern is added to the leftmost end, this function is an external function. The C language specifies that if extern is omitted during definition, it is implicitly an external function. The internal function must be preceded by the static keyword;
In the file that needs to call this function, use extern to declare the function, indicating that this function is an external function defined in other files.
Meaning of "C": (extern "C ")
C ++ supports the overload mechanism through different types of function parameters. The Compiler generates different internal identifiers for each function based on parameters. For example, the compiler is void Eat (Beef ...); Void Eat (Fish ...); Void Eat (Chicken ...); The three Eat functions generate internal identifiers such as _ eat_beef, _ eat_fish, and _ eat_chicken (different compilers may generate internal identifiers of different styles ).
What if a C ++ program needs to call a compiled C function?
Assume that the declaration of a C function is as follows:

 foo( x,  y);

After the function is compiled by the C compiler, its name in the library is _ foo, while the C ++ compiler generates names such as _ foo_int_int to support function overloading and secure type connection. C ++ programs cannot directly call C functions because their compiled names are different. C ++ provides a C connection to exchange the specified symbol extern "C" to solve this problem.
For example:

 foo( x, }

This tells C ++ to compile the interpreter. The function foo is a C connection. You should find the name _ foo in the library instead of _ foo_int_int. The C ++ compiler developer has processed the header files of the C standard library as extern "C", so we can use # include to directly reference these header files.

Suppose there is a C file:

 _C_H_  add( x, 
 add( x,  x+

Call the add () function in C ++

#include <iostream> , 

Error: the external symbol "int _ cdecl add (int, int )"(? Add @ YAHHH @ Z), which is referenced in function _ main.
To solve this problem, we need to use extern "C ". Rewrite the C file

 _C_H_   add( x, 

The file is *. c ,__ cplusplus is not defined, and extern "C" {} does not take effect at this time. For c, only extern int add (int, int) is used, while c ++ source files are compiled, __cplusplus is defined. For C ++, if he sees extern "C" {extern int add (int, int) ;}, the compiler will know add (1, 0) the C connection is called.
: Many DLL generated files (XXX. c) extern "C" is often found. In windows, a dll is created using C language compilation. C Programs can call the DLL correctly. When a user uses c ++ to call the DLL, extern "C" {} takes effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.