Difference between extern and extern "C"
(1) extern
Anyone who has learned C/C ++ (cplusplus/cpp) knows that extern is an attribute in programming languages. It represents the scope (visibility) attributes of variables, functions, and other types, is a keyword in a programming language. During compilation, this keyword tells the compiler that the declared functions and variables can be used in this module or file and other modules or files. Generally, programmers only use this keyword in the "* h" (header file) to limit attributes of variables, functions, and other types, and then use it in other modules or this module, such:
/* File1.h */
Extern int I;
/* Call this variable in other file2.c files */
[Cpp]
Int welcom (void)
{
If (I> 0)
{
Printf ("Hello World! \ N ");
}
}
(2) extern "C"
When calling C code, C ++ uses extern "C", extern "C" to indicate a compilation protocol.
Extern "C"
{
/* C language code segment compiled using the C compiler */
}
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, but all adopt the same mechanism, and the new name is called "mangledname "). 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 ++, the void foo (int x, int y) and void foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.
Compilation Method when the extern "C" clause is added
Extern "C"
{
Void foo (int x, int y );
}
Because void foo (int x, int y) is specified and compiled using the C compiler, the name in the symbol library is _ foo. You can find the foo implementation when linking the C implementation file, otherwise the compiler will prompt undefine reference foo.
Sample Code
Foo. h
[Cpp]
# Ifdef _ cplusplus/* If C ++ is used, the following code uses the C compiler ;*/
Extern "C"/* If C ++ is not used, precompile the code segment in sequence */
{
# Endif
/* C language code segment compiled using the C compiler */
Extern int foo (int x, int y );
# Ifdef _ cplusplus/* end with C compiler */
}
# Endif
Foo. c foo. h's C implementation file
[Cpp]
# Include "foo. h"
Int foo (int x, int y)
{
Return x + y;
}
Test. cpp C ++ calls the C Function
[Cpp]
# Include "foo. h"
# Include <stdio. h>
Int main ()
{
Printf ("Hello % d \ n", foo (1, 2 ));
Return 0;
} Www.2cto.com
Gcc-c foo. c
G ++-c test. cpp
G ++ test. o foo. o-o test
#./Test
Hello 3