extern "C" usage parsing

Source: Internet
Author: User

extern "C" usage parsing

Author author Jason Ding , Link http://www.jianshu.com/p/5d2eeeb93590

Introduction

C + + retains some of the characteristics of a procedural language, so it can define global variables and functions that are not part of any class. However, after all, C + + is an object-oriented programming language, in order to support the overloading of functions, C + + to the global function of the processing method and C are obviously different.
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.
For example, you have developed a DLL library with C, in order to enable the C + + language to call your DLL output (export) function, you need to use extern "C" to force the compiler not to modify your function name.
Secret extern "C"
Speaking from the standard header file

#ifndef __incvxworksh/*prevent the header file from being repeatedly referenced*/#define__incvxworksh#ifdef __cplusplus//__cplusplus is a custom macro in CPPextern "C"{//tell the compiler that this part of the code is compiled in the C language format, not the C + +#endif    /** * * * Some declaration or so * * * **/#ifdef __cplusplus}#endif#endif/* __incvxworksh */


the meaning of extern "C"

extern "C" contains a double meaning, literally: First, the object it modifies is "extern", and secondly, the target it modifies is "C".
The function or variable defined by extern "C" is of type extern;
1. extern key word
extern is a keyword that indicates the scope (visibility) of functions and global variables in the C/s + + language, which tells the compiler that its declared functions and variables can be used in this module or in other modules.
Typically, the function and global variables that this module provides to other modules in the header file of the module are declared with the keyword extern. For example, if module B is to reference the global variables and functions defined in module A, only the header file of module A can be included. In this way, when a 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 this function in the link stage from the target code generated by module a compilation.
The keyword that corresponds to extern is static, and the global variables and functions it modifies can only be used in this module. Therefore, a function or variable may not be modified by extern "C" only if it is used by this module.

2. Variables and functions modified by extern "C" are compiled and linked in C language.
Let's start with a look at how C + + is compiled for a function that is similar to.
as an object-oriented language, C + + supports function overloading, whereas programming language C is not supported. Functions are compiled in C + + with different names in the symbol library than in the C language. For example, suppose a function is prototyped as:
void foo (int x, int y);
The function is compiled by the C compiler in the symbol library with the name _foo, while the C + + compiler produces names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, and the resulting new name is called "Mangled Name").
_foo_int_int Such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading. For example, in C + +, the function void foo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float.
Similarly, variables in C + + support class member variables and global variables in addition to local variables. The class member variable of the program that the user writes may have the same name as the global variable, and we use the "." to differentiate. In essence, the compiler, when compiling, is similar to the processing of the function and takes a unique name for the variable in the class, which differs from the name of the global variable named in the user program.

3. Examples and explanations
(1) connection method without extern "C" declaration
assume that in C + +, the header file for module A is as follows:

 //  module a header file ModuleA.h   #ifndef module_a_h   #define  module_a_hint  foo (int  x, int   y);   #endif  //  reference the function in module B:  //  module B implements the file ModuleB.cpp  #include  modulea.h   foo ( 2 , 3 );  


In fact, during the connection phase, the linker looks for symbols such as _foo_int_int from the target file Modulea.obj generated by module A!

(2) Add extern "C" after the declaration of the compile and link mode
After adding the extern "C" declaration, the header file of module a becomes:

// module A header file ModuleA.h #ifndef module_a_h #define Module_a_hextern"C"int int int y) ; #endif


Foo (2,3) is still called in the implementation file of Module B, and the result is:

<1>a compile to generate the target code of Foo, the name is not special processing, the use of C language method;

<2> the linker is looking for an unmodified symbol name _foo when looking for foo (2,3) calls for the target code of Module B.

If the function in module a declares Foo to be an extern "C" type, and Module B contains an extern int foo (int x, int y), module B cannot find the function in module A, and vice versa.

The true purpose of the extern "C" statement is to achieve mixed programming of C + + and C and other languages.


Application situations

C + + code calls the code, and uses it in the header file of C + +
In C + +, references to functions and variables in the C language require the following processing when the C language header file is included (assuming cExample.h):

    extern " C "     {    "cExample.h"    }


In the header file of the C language, only the extern type is specified for its external function, and the extern "C" declaration is not supported in the C language, and a compile syntax error occurs when the. c file contains the extern "C".

/*C Language Header file: cExample.h*/#ifndef C_example_h#defineC_example_hextern intAddintXinty);//Note: written as extern "C" int add (int, int);#endif/*C Language Implementation file: Cexample.c*/#include"cExample.h"intAddintXinty) {returnX +y;}//C + + implementation file, call Add:cppFile.cppextern "C"{#include"cExample.h"        //Note: This is not appropriate, if this compile pass, instead, replace with extern "C" int add (int, int);}intMainintargcChar*argv[]) {Add (2,3); return 0;}


If C + + calls a. dll written in C, the extern "C" {} should be added when the header file for the. dll is included or the interface function is declared.

When referencing functions and variables in the C + + language, the header file of C + + needs to be added extern "C", but the header file that declares extern "C" cannot be directly referenced in C, it should only be declared as extern "C" functions defined in C + +

//C + + header file CppExample.h#ifndef Cpp_example_h#defineCpp_example_hextern "C" intAddintXinty);#endif//C + + implementation file CppExample.cpp#include"cppExample.h"intAddintXinty) {returnX +y;}/*C Implementation file cfile.c/* This compiles an error: #include "cExample.h"*/extern intAddintXinty);intMainintargcChar*argv[]) {Add (2,3 ); return 0;}

extern "C" usage parsing

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.