Question about extern

Source: Internet
Author: User

Example:

Two files:

C file: C. c

 

 

**************************************** *******

Int external = 5; // global variable. The default value is extern.
Int func () // global function. The default value is extern.
{
Return external;
}

**************************************** *******

Cpp file: CPP. cpp

**************************************** *******

# Include "iostream"
Using namespace std;

# Ifdef _ cplusplus
Extern "C"
{
# Endif
Extern int external; // tell the compiler that extern is an int defined in another file.No storage space is allocated to it..
Extern int func (); // although both are in {} of extern "C ",Explicitly specify externOtherwise, an error is returned.
# Ifdef _ cplusplus // not only functions, but also variables in extern "C.
}
# Endif

Void main (void)
{
Cout <"the value of external in c file is:" <external <endl;
External = 10;
Cout <"after modified in cpp is:" <func () <endl;
}

**************************************** *******

Before discussing extern "C", we will first discuss extern.

1,In C/C ++, global variables and global functions are defined by extern by default.That is, if A project is composed of several files, global variables or global functions are defined in the global scope of File A, which is visible in other files by default, that is, if other files explicitly declare global variables and functions in A with extern in their global scopes, they can use the global variables and functions defined in.

2. Note that declarations such as extern int external do not allocate memory space for external. Here is just a declaration, which tells the compiler that the external variable is defined in other files. Therefore, no error is reported even if the external definition cannot be found during compilation, because the external is found in the obj of other files during the connection.

3. cout <"the value of external in c file is:" <external <endl;
External = 10;
Cout <"after modified in cpp is:" <func () <endl;

Use these three codes to describe extern: The variable external is in C. CPP defined in file c. cpp just declares that it tells the compiler that the external used in CPP is defined in other files. First, print out the external value, which is 5 (externl is defined as 5 in the C file, which means that CPP does not open a new space for external, otherwise it should be 0). Then, change external to 10 in the CPP file and call func () to explicitly display the value of external. Func () is a function in C. It returns the value of external, and the result is printed as 10 (further explained that external is defined in C and used in CPP ).

4. If the extern before the variable is removed during declaration, a connection error occurs, prompting that two variables with the same name are defined.

5. The corresponding extern is static. Using static explicitly declared variables and functions in the global scope indicates that they can only be used by the file where they are located, and other files are invisible to them. If a static variable is declared in the module, the scope of the variable is still the module, but the lifecycle is extended until the end of the program.

Let's take a look at extern "C ".

The reference of extern "C" is to solve the Mixed Programming Problem between C ++ and C.

The function overload mechanism supported by C ++ makes C and C ++ compilation different.

For example

Void foo (int x, int y );

After being compiled by the C compiler, the name of the symbol library is _ foo, while that of the C ++ compiler is _ foo_int_int, which is called mangled name. C ++ relies on this compilation mechanism to implement overloading. For example, if another overload function void foo (int x, floaty) is used, the name of the compiled Symbol Library is _ foo_int_float.

This makes it easy to understand that if a function written and compiled in C needs to be called and used in a C ++ program, extern "C" must be used ", because extern "C" can ensure that the called C function is compiled in C mode; otherwise, because the name of the compiled Symbol Library does not match, if the connector does not find a matching function in obj of C, a connection error occurs.

It is worth noting that not only global functions, but also global variables defined in C should be placed in {} of extern "C.

You can also add the header file (# include) of the entire C file in extern "C ).

Because C does not support extern "C", the Conditional compilation Statement (# ifdef... # endif) in the source file appears ).

How to view the symbol library in the obj file:

There is an application dumpbin.exe under the bindirectory of the vcss installation directory. Use the following command line statement:

Dumpbin.exe filename. obj/symbols/out: show.txt (/out: show.txt is to redirect the output result to the show.txt file)

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.