An explanation of the role of extern "C"

Source: Internet
Author: User

The main purpose of extern "C" is to be able to correctly implement C + + code calls to other C language code. The addition of extern "C" instructs the compiler to compile this part of the code in C instead of C + +. Because C + + supports function overloading, the compiler compiles the function by adding the parameter type of the function to the compiled code, not just the function name, and the C language does not support the functions overloading, so the function of compiling C code does not take the parameter type of the function, generally including the function name.

This feature is very useful, because before the advent of C + +, a lot of code is written in C, and the very bottom of the library is written in C, in order to better support the original C code and the written C language library, need to support C + + as far as possible, and extern "C" is one of the strategies.

This function is mainly used in the following situations:

1. C + + code calling code

2. Use in C + + header files

3, in the co-development of many people, maybe some people are good at C language, and some people are good at C + +, such circumstances will also be useful to

Give me an example of my design:

Modulea, Moduleb two modules, B calls the code in a, where a is implemented in C, and B is implemented using C + +, the following is an implementation method:

Modulea header File

#ifndef __module_a_h//For module A, this macro is to prevent duplicate references to the header file

#define __module_a_h

int fun (int, int);

#endif

Modulea implementation of File MODULEA.C//module A does not change

#include "Modulea"

int fun (int a, int b)

{

return a+b;

}

Moduleb header File

#idndef __module_b_h//Obviously this part is also to prevent duplicate references

#define __module_b_h

#ifdef __cplusplus//And this part is to tell 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), it is compiled in the C language way

#include "ModuleA.h"

#endif

...//other code

#ifdef __cplusplus

}

#endif

#endif

Moduleb implementation of the file ModuleB.cpp//b module has not changed, but the design of the header file has changed

#include "ModuleB.h"

int main ()

{

Cout<<fun (2,3) <<endl;

}

The following is a detailed description:

Because C, C + + compiler is not exactly the same as the compilation of functions, especially for C + +, supporting the overloading of functions, compiled functions are generally named after the function name and parameter type.

For example, the function void fun (int, int), compiled may be (different compiler results are different) _fun_int_int (different compilers may be different, but all adopt a similar mechanism, using the function name and parameter type to name the compiled function name), and C language does not have a similar overloaded mechanism, Typically, the function name is used to indicate the compiled function name, and the corresponding function may be a name such as _fun.

Look at one of the following interview questions: Why does the standard header file have a similar structure?

#ifndef __incvxworksh/* Prevents the header file from being re-referenced */

#define __incvxworksh

#ifdef __cplusplus//tells the compiler that this part of the code is compiled in the C language format instead of C + +

extern "C" {

#endif

/*...*/

#ifdef __cplusplus

}

#endif

#endif/*end of __incvxworksh*/

Analysis:

    • Obviously, the compiler macro "#ifndef __incvxworksh, #define __incvxworksh, #endif" (The blue part of the above code) in the header file is intended to prevent the header file from being repeatedly referenced
    • So

#ifdef __cplusplus, where __cplusplus is a custom macro in CPP!!! )

extern "C" {

#endif

#ifdef __cplusplus

}

#endif

What is the role of it?

The extern "C" contains a double meaning, literally knowing that, first of all, the target being modified by it is "extern", and secondly, the target code that it modifies is "C".

    • A function or variable defined by extern "C" is an extern type.

extern is a keyword that indicates the scope of functions and global variables in the C + + language, which tells the compiler that its declared functions and variables can be used in this module or in other modules.

Remember, the following statement:

extern int A; is simply a declaration of a variable that does not define variable A and does not allocate space for a. Variable A can only be defined once in all modules as a global variable, otherwise an error occurs.

In general, the functions and global variables that this module provides to other modules in the header file of the module are given the keyword extern life. For example, if module B is to refer to the global variables and functions defined in module A, only the header file of module A can be included. When the function in module A is called in Module B, in the compile phase, Module B cannot find the function, but it does not error; it will find the function in the link stage from the target code generated by module a compilation.

extern corresponds to a keyword that static,static indicates that a variable or function can only be used in this module, so a static modified variable or function cannot be modified by extern c.

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

As mentioned above, because C + + support function overloading, and C language is not supported, so the function is compiled in C + + in the symbol library name is different from the C language; C + + compiled functions need to add parameter type to uniquely calibrate the overloaded function, and with extern "C", is to indicate to the compiler that this code compiles in C language.

How to link without extern "C" declaration:

Module a header file moduleA.h

#idndef _module_a_h

#define _module_a_h

int foo (int x, int y);

#endif

This function is called in Module B:

Module B implementation file ModuleB.cpp

#include "ModuleA.h"

Foo (2,3);

In fact, during the link phase, the connector will look for _foo_int_int such symbols from the target file Modulea.obj generated by module A!!! , obviously this is impossible to find, because the Foo () function is compiled into a _foo symbol, so there is a link error.

A common practice is to refer to one of the following implementations:

Modulea, Moduleb two modules, B calls the code in a, where a is implemented in C, and B is implemented using C + +, the following is an implementation method:

Modulea header File

#ifndef __module_a_h//For module A, this macro is to prevent duplicate references to the header file

#define __module_a_h

int fun (int, int);

#endif

Modulea implementation of File MODULEA.C//module A does not change

#include "Modulea"

int fun (int a, int b)

{

return a+b;

}

Moduleb header File

#idndef __module_b_h//Obviously this part is also to prevent duplicate references

#define __module_b_h

#ifdef __cplusplus//And this part is to tell 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), it is compiled in the C language way

#include "ModuleA.h"

#endif

...//other code

#ifdef __cplusplus

}

#endif

#endif

Moduleb implementation of the file ModuleB.cpp//b module has not changed, but the design of the header file has changed

#include "ModuleB.h"

int main ()

{

Cout<<fun (2,3) <<endl;

}

The use point of extern "C"

1. Can be a single statement

extern "C" double sqrt (double);

2. Can be a compound statement, equivalent to the declaration in the compound statement is added extern "C"

extern "C"

{

Double sqrt (double);

int min (int, int);

}

3. Can contain header files, equivalent to the declaration in the header file is added extern "C"

extern "C"

{

#i nclude <cmath>

}

4. The extern "C" cannot be added inside the function

5. If a function has more than one declaration, you can either add extern "C", or it can appear only in the first declaration, and subsequent declarations will accept the rule of the first link indicator.

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

Reference article:

extern C uses

The role of extern C "turn" _ See snow.

extern c key points of use

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

An explanation of the role of extern "C"

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.