Explanation of the functions of extern C and extern

Source: Internet
Author: User

Explanation of the functions of extern C and extern

The main function of extern "C" is to call other C code correctly. After extern "C" is added, it indicates that the code of the compiler is compiled in C language instead of C ++. C ++ supports function overloading. Therefore, during function compilation, the parameter types of the function are added to the compiled code, not just the function name; C language does not support function overloading. Therefore, when compiling a function in C language code, the parameter type of the function is not included. Generally, the function name is included.

This function is very useful, because before the emergence of C ++, many codes were written in C language, and the underlying library was also written in C language, to better support the original C code and the written C language library, we need to support C as much as possible in C ++, and extern "C" is one of the strategies.

This function is mainly used in the following situations:

1. C ++ code calls C language code

2. Use it in the header file of C ++

3. Some people may be good at C language and some others are good at C ++ in collaborative development, which will be used in this case.

Here is an example of my design:

ModuleA and moduleB are two modules. B calls the code in A, where A is implemented in C language and B is implemented in C ++. The following provides an implementation method:

// ModuleA header file

# Ifndef _ MODULE_A_H // For module A, this macro is used to prevent repeated references to header files.

# Define _ MODULE_A_H

Int fun (int, int );

# Endif

// ModuleA implementation file moduleA. C // The implementation part of module A has not changed

# Include "moduleA"

Int fun (int a, int B)

{

Return a + B;

}

// ModuleB header file

# Idndef _ MODULE_ B _H // This part is obviously used to prevent repeated references.

# Define _ MODULE_ B _H

# Ifdef _ cplusplus // This part tells the compiler that if _ cplusplus is defined (that is, if it is a cpp file, extern "C" {// because the cpp file defines the macro by default), the C language is used for compilation.

# Include "moduleA. h"

# Endif

... // Other code

# Ifdef _ cplusplus

}

# Endif

# Endif

// ModuleB implementation file moduleB. cpp // The implementation of Module B has not changed, but the header file design has changed.

# Include "moduleB. h"

Int main ()

{

Cout <fun (2, 3) <endl;

}

The following is a detailed introduction:

Since C and C ++ compilers compile and process functions differently, especially C ++ supports function overloading, compiled functions are generally named after the function name and the form parameter type.

For example, the void fun (int, int) function, after compilation, may be (different compiler results) _ fun_int_int (different compilers may be different, but they all adopt a similar mechanism, use the function name and parameter type to name the compiled function name). The C language does not have a similar overload mechanism. Generally, the compiled function name is used to specify the name, corresponding to the above function may be a name like _ fun.

Let's take a look at the following interview question: why do standard header files have similar structures?

# Ifndef _ INCvxWorksh/* prevent this header file from being repeatedly referenced */

# Define _ INCvxWorksh

# Ifdef _ cplusplus // tell the compiler that this part of the code is compiled in the C language format, instead of the C ++

Extern "C "{

# Endif

/*... */

# Ifdef _ cplusplus

}

# Endif

# Endif/* end of _ INCvxWorksh */

Analysis:

  • Obviously, the function of compiling the macro "# ifndef _ INCvxWorksh, # define _ INCvxWorksh, and # endif" (the blue part in the code above) in the header file is to prevent the header file from being repeatedly referenced.
  • So

# Ifdef _ cplusplus (_ cplusplus is a custom macro in cpp !!!)

Extern "C "{

# Endif

# Ifdef _ cplusplus

}

# Endif

What is the role?

Extern "C" contains double meanings. It can be understood literally. First, the target to be modified is "extern". Second, the target code modified by it is "C.

  • The function or variable specified by extern "C" is of the extern type.

Extern is a keyword in the C/C ++ language that indicates the scope of the function 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 statement:

Extern int a; it is just a declaration of a variable. It is not defining variable a and does not allocate space for variable. Variable a can only be defined once as a global variable in all modules; otherwise, an error occurs.

In general, the function and global variables provided by this module to other modules in the module header file are lived with the keyword extern. For example, to reference the global variables and functions defined in module A, Module B only needs 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, no error is reported; it will find this function from the target Code Compiled by module A in the Link phase.

The keyword corresponding to extern is static. static indicates that variables or functions can only be used in this module. Therefore, variables or functions modified by static cannot be modified by extern C.

  • Variables and functions modified by extern "C" are compiled and linked in C language: this is very important !!!!

As mentioned above, C ++ supports function overloading, while C does not. Therefore, the name of a function in the symbol library compiled by C ++ is different from that in C; C ++ compiled functions must be added with the parameter type to uniquely calibrate the overloaded function. After the extern "C" is added, to indicate to the compiler that the code is compiled in C language.

Link Method when the extern "C" declaration is not added:

// Module A header file moduleA. h

# Idndef _ MODULE_A_H

# Define _ MODULE_A_H

Int foo (int x, int y );

# Endif

Call this function in Module B:

// Module B implements the file moduleB. cpp

# Include "moduleA. h"

Foo (2, 3 );

In fact, in the connection phase, the connector will find symbols such as _ foo_int_int from the target file moduleA. obj generated by module !!!, Obviously this is not possible, because the foo () function is compiled into the _ foo symbol, so a link error occurs.

For common practices, refer to the following implementation:

ModuleA and moduleB are two modules. B calls the code in A, where A is implemented in C language and B is implemented in C ++. The following provides an implementation method:

// ModuleA header file

# Ifndef _ MODULE_A_H // For module A, this macro is used to prevent repeated references to header files.

# Define _ MODULE_A_H

Int fun (int, int );

# Endif

// ModuleA implementation file moduleA. C // The implementation part of module A has not changed

# Include "moduleA"

Int fun (int a, int B)

{

Return a + B;

}

// ModuleB header file

# Idndef _ MODULE_ B _H // This part is obviously used to prevent repeated references.

# Define _ MODULE_ B _H

# Ifdef _ cplusplus // This part tells the compiler that if _ cplusplus is defined (that is, if it is a cpp file, extern "C" {// because the cpp file defines the macro by default), the C language is used for compilation.

# Include "moduleA. h"

# Endif

... // Other code

# Ifdef _ cplusplus

}

# Endif

# Endif

// ModuleB implementation file moduleB. cpp // The implementation of Module B has not changed, but the header file design has changed.

# Include "moduleB. h"

Int main ()

{

Cout <fun (2, 3) <endl;

}

Usage of extern "C"

1. It can be a single statement

Extern "C" double sqrt (double );

2. It can be a composite Statement, which is equivalent to adding extern "C" to all declarations in the composite statement"

Extern "C"

{

Double sqrt (double );

Int min (int, int );

}

3. the header file can be included, which is equivalent to adding extern "C" to all declarations in the header file"

Extern "C"

{

# I nclude <cmath>

}

4. You cannot add extern "C" to the function.

5. If a function has multiple declarations, you can add extern "C" to them, or you can only add them to the first declaration. The subsequent declaration will accept the rules of the first link indicator.

6. In addition to extern "C", there are extern "FORTRAN" and so on.

References:

Use of extern C

Function of extern c ..

Extern C usage highlights

Http://blog.chinaunix.net/u/29619/showart_230148.html

Http://blog.csdn.net/weiqubo/archive/2009/10/16/4681813.aspx

Http://hi.baidu.com/sundl2268/blog/item/4969453d2258bae53c6d970a.html

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.