Detailed description of the functions of extern C

Source: Internet
Author: User

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

Detailed description of the functions of extern C

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.