About extern c

Source: Internet
Author: User

The original intention of the C ++ language is "a better
C. as a language compatible with C, C ++ retains a part of the process
Style language features (known as "not completely Ground Object"), so it can define global variables and functions that do not belong to any class. however, C ++ is an object-oriented programming language.
With function overloading, C ++ treats global functions differently from C.

2. Start with the standard header file

An enterprise once gave the following interview questions:

Interview Questions

Why do standard header files have a structure similar to the following?

# Ifndef _ INCvxWorksh

# Define _ INCvxWorksh

# Ifdef _ cplusplus

Extern "C "{

# Endif

/*...*/

# Ifdef _ cplusplus

}

# Endif

# Endif/* _ INCvxWorksh */

Analysis

Obviously, the compilation macro "# ifndef _ INCvxWorksh, # define _ INCvxWorksh, and # endif" in the header file is used to prevent the header file from being repeatedly referenced.

So

# Ifdef _ cplusplus

Extern "C "{

# Endif

# Ifdef _ cplusplus

}

# Endif

What is the role? We will discuss it one by one below.

3. Deep encryption extern "C"

Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C. let's explain these two meanings in detail.

(1) functions or variables limited by extern "C" are of the extern type;

Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. remember, the following statements:

Extern int;

It is just a declaration of a variable. It does not define variable a and does not allocate memory space for variable. variable a can only be defined once as a global variable in all modules; otherwise, a connection error may occur.

In general, the function and global variables provided by this module to other modules in the module header file are declared with the keyword extern. for example, if Module B wants to reference the global variables and functions defined in module
You only need to include the header file of module. in this way, when Module B calls A function in module A, although Module B cannot find the function in the compilation phase, it does not report an error. It will compile the function from module A in the connection phase.
Find this function in the generated target code.

The keyword corresponding to extern is static. 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 and cannot be modified by extern "C.

(2) variables and functions modified by extern "C" are compiled and connected in C language;

Compilation Method without the extern "C" declaration

First, let's take a look at how C-like functions are compiled in C ++.

As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. the name of the function in the symbol library after being compiled by C ++ is different from that in the C language. for example, assume that the prototype of a function is:

Void foo (int x, int y );

After the function is compiled by the C compiler, its name in the symbol library is _ foo, while the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names,
Both adopt the same mechanism, and the new name is called "mangled
Name "). A name such as _ foo_int_int contains the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading. for example, in C ++
Void foo (int x, int y) and void foo (int x, float y
) The symbols generated by compilation are different, and the latter is _ foo_int_float.

Similarly, variables in C ++ support both local variables and class member variables and global variables. the class member variables of the program written by the user may have the same name as the global variables ". "to distinguish. essentially,
During compilation, the compiler has a unique name for the variables in the class similar to the function processing. This name is different from the global variable name with the same name in the user program.

Connection method when extern "C" is not added

Suppose in C ++, the header file of module A is as follows:

// Module A header file moduleA. h

# Ifndef MODULE_A_H

# Define MODULE_A_H

Int foo (int x, int y );

# Endif

Reference this function in Module B:

// Module B implements the file moduleB. cpp

# I nclude "moduleA. h"

Foo (2, 3 );

In fact, in the connection phase, the connector looks for symbols such as _ foo_int_int from the target file moduleA. obj generated by module!

Compilation and Connection Methods After the extern "C" clause is added

After the extern "C" statement is added, the header file of module A is changed:

// Module A header file moduleA. h

# Ifndef MODULE_A_H

# Define MODULE_A_H

Extern "C" int foo (int x, int y );

# Endif

In Module B's implementation file, foo (2, 3) is still called. The result is:

(1) When module A compiles and generates the foo target code, it does not perform special processing on its name and uses the C language;

(2) When the connector looks for the foo (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ foo.

If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int foo (int x, int y ), module B cannot find the function in module A, and vice versa.

Therefore, an extern can be summarized in one sentence.
The true purpose of the "C" statement (the birth of any syntactic feature in any language is not random, and is driven by the needs of the real world. when thinking about the problem, we can't just stay in this language.
We also need to ask why it is doing this and what the motivation is, so that we can better understand many questions ):

Realize mixed programming of C ++, C and other languages.

Understand the motivation for setting up extern "C" in C ++. Next we will analyze the general usage skills of extern "C.

4. Usage of extern "C"

(1) To reference functions and variables in C language in C ++, the following processing must be performed when the C Language header file (assumed as cExample. h) is included:

Extern "C"

{

# I nclude "cExample. h"

}

In the header file of the C language, the external function can only be specified as the extern type. The C language does not support the extern "C" declaration. when the c file contains extern "C", a compilation syntax error occurs.

The source code of the three files in the example project of C ++ referenced by the author is as follows:

/* C header file: cExample. h */

# Ifndef C_EXAMPLE_H

# Define C_EXAMPLE_H

Extern int add (int x, int y );

# Endif

/* C language implementation file: cExample. c */

# I nclude "cExample. h"

Int add (int x, int y)

{

Return x + y;

}

// C ++ implementation file, call add: cppFile. cpp

Extern "C"

{

# I nclude "cExample. h"

}

Int main (int argc, char * argv [])

{

Add (2, 3 );

Return 0;

}

If C ++ calls a. DLL file written in C language, when it includes the header file of. DLL or the declared interface function, add extern "C "{}.

(2) When referencing functions and variables in C ++ in C, the header file of C ++ needs to add extern "C ", however, you cannot directly reference this header file that declares extern "C" in C. You should only declare the extern "C" function defined in C ++ as the extern type.

The source code of the three files contained in the example project of C ++ is as follows:

// C ++ header file cppExample. h

# Ifndef CPP_EXAMPLE_H

# Define CPP_EXAMPLE_H

Extern "C" int add (int x, int y );

# Endif

// C ++ implementation file cppExample. cpp

# I nclude "cppExample. h"

Int add (int x, int y)

{

Return x + y;

}

/* C implementation file cFile. c

/* Compilation errors: # I nclude "cExample. h "*/

Extern int add (int x, int y );

Int main (int argc, char * argv [])

{

Add (2, 3 );

Return 0;

}

If you thoroughly understand the role of extern "C" described in section 3rd in the compilation and connection phases, you can really understand the usage of referencing C Functions and C ++ functions from C ++ described in this section. pay special attention to the details of the sample code given in section 4th.

See the following code in the cpp Code:

# Ifdef _ cplusplus
Extern "C "{
# Endif

// A piece of code

# Ifdef _ cplusplus
}
# Endif

What does this code mean? First, __cplusplus is the custom macro in cpp. If this macro is defined, it indicates that this is a piece of cpp Code, that is, the above Code
The meaning is: if this is a piece of cpp Code, add extern "C" {And} to process the code.

Understand Why extern is used
"C", we have to start with the overload processing of functions in cpp. in c ++, in order to support the overload mechanism, some processing of the function name should be performed in the compilation code, such as function return.
Return type, etc. In C, it is just a simple function name and no other information is added. That is to say, the processing of the generated function names in C ++ and C is different.

For example, in the following simple function, let's look at the changes in the assembly code generated by adding or not adding extern "C:

Int f (void)
{
Return 1;
}
The assembly code generated when you add 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
But after extern "C" is not added

. File "test. cxx"
. Text
. Align 2
. Globl _ Z1fv
. Def _ Z1fv;. scl 2;. type 32;. endef
_ Z1fv:
Pushl % ebp
Movl % esp, % ebp
Movl $1, % eax
Popl % ebp
Ret
The two pieces of assembly code are also generated using the gcc-S command. All parts are the same, except the generated function name. One is _ f and the other is _ Z1fv.

We understand the impact of adding and not adding extern "C" to the function name. Let's continue our discussion: Why do we need to use extern?
What about "C? When designing C ++, the father of C ++ considered that a large number of C Code existed at that time. In order to support the original C code and write the C library, C ++ should be supported as much as possible.
Extern "C" is a policy.


Think about this situation: a library file has been written in C and runs well. In this case, we need to use this library file, however, we need to use C ++ to write this new code. if this code causes
If you use C ++ to link the C library file, a link error occurs. let's look at a piece of code: First, we use the C processing method to write a function, that is, assume that this function was
It is written in C:

// F1.c
Extern "C"
{
Void f1 ()
{
Return;
}
}
The compilation command is: gcc-c f1.c-o f1.o generates a library file named f1.o. Then write a code to call this f1 function:

// Test. cxx
// This extern indicates that the f1 function is defined elsewhere.
// Compile, but the link is still required
// Link the original library file.
Extern void f1 ();

Int main ()
{
F1 ();

Return 0;
}
Through gcc-c test. cxx-o test. o generates a file named test. o file. then, we use gcc test. o f1.o is used to link two files, but an error occurs. The error message is:

Test. o (. text + 0x1f): test. cxx: undefine reference to 'f1 ()'
That is to say, when compiling test. in cxx, the compiler uses the C ++ method to process the f1 () function, but actually the Linked Library File uses the C method to process the function, so there will be a link error: Because the linker cannot find the function.

Therefore, to call the library file written in C ++ code, we need to use extern "C" to tell the compiler: This is a library file written in C, please use the C method to link them.

For example, now we have a C library file whose header file is f. h. The generated lib file is f. lib, if we want to use this library file in C ++, We need to write as follows:

Extern "C"
{
# Include "f. h"
}
Back to the problem above, if you want to correct the Link error, we need to rewrite test. cxx like this:

Extern "C"
{
Extern void f1 ();
}

Int main ()
{
F1 ();

Return 0;
}
Recompile and the link will pass.

Summary

C and C ++ process functions differently. extern "C" is a means to enable C ++ to call library files written by C. If you want to prompt the compiler to use the C method to process functions, use extern "C" to describe.

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.