[Reserved] extern "C" to resolve usage

This article reprinted from: http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html
 
1. Introduction
  Create the original intention of the C ++ language is "a better C", but this does not mean that compiling and linking C language exactly the same way as C ++, a C-like language of global variables and functions used. As a desire compatible with C language, C ++ process retains some of the features of the language (the world is referred to as "object does not completely face"), so it can not belong to any class defined global variables and functions. However, C ++, after all, is an object-oriented programming language, in order to support function overloading, C ++ has a significantly different way of handling C global function.
 
2. from the standard header files Speaking
  Some companies have given below a face questions:
  Interview questions
  Why is the standard header file has a structure similar to the following?
 
 1 #ifndef __INCvxWorksh
 2 #define __INCvxWorksh
 3 #ifdef __cplusplus
 4 extern "C" {
 5 #endif
 6     /*...*/
 7 #ifdef __cplusplus
 8 }
 9 #endif
10 #endif /* __INCvxWorksh */

      analysis

  Clearly, the header file to compile macros "#ifndef __INCvxWorksh, # define __INCvxWorksh, # endif" is to prevent the header file from being referenced.
  Then
1 #ifdef __cplusplus
2 extern "C" {
3  #endif
4  #ifdef __cplusplus
5 }
6 #endif
  What role is it? We will be one by one below.
 
3. Deep Inside extern "C"
  extern "C" contains a double meaning, you can get literally: First of all, it was modified goal is to "extern"; second, it was modified goal is a "C". Let's take a detailed interpretation of both heavy meaning.
  Function or variable is defined extern "C" type is extern;
  extern is C / C ++ language indicated scope functions and global variables (visibility) of the keyword, which tells the compiler function and variable declarations that may be used in the other module or modules. Remember, the following statements:
  extern int a;
  Just declare a variable, which is not in the definition of a variable, not to allocate a memory space. A variable as a global variable in all modules can only be defined once, or the connection error.
  Typically, in the header file module of this module available to other modules reference functions and global variables with the keyword extern statement. For example, if the referenced module B For global variables and functions defined in the module A can just include header module A. Thus, a function module A module B calls at compile time, although not find the function module B, but not given; it is generated from the A phase in the connection module compiled object code for this function is found in .
  Extern corresponding to the keyword is static, it is modified only global variables and functions used in this module. Accordingly, a variable or function is only possible when using this module, which can not be extern "C" modification.
  Modified extern "C" variables and functions in accordance with C language and compiled connection;
  Compilation mode when not adding extern "C" statement
  First, take a look at C ++, a function similar to how C is compiled.
  As an object-oriented language, C ++ supports function overloading, the procedural language C is not supported. Function is different from the name of the C language in the symbol library after C ++ compiler. For example, suppose a function prototype is:
void foo( int x, int y );
  This function is the C compiler in the name of the symbol library is the _foo, and the C ++ compiler will produce _foo_int_int such as the name (different compiler may generate different names, but all using the same mechanism generating new name called "mangled name").
  Such names _foo_int_int contains the function name, the number of function parameters and the type of information, C ++ is to rely on this mechanism to implement overloaded functions. For example, in C ++, the function void foo (int x, int y) and the void foo (int x, float y) compiler generated symbol is not the same, the latter _foo_int_float.
  Similarly, C ++ variables in addition to supporting the local variables, but also supports class member variables and global variables. Class member variables may be written by the user program with the same name as a global variable, we have to. "" Distinguished. And essentially the compiler during compilation, processing similar function, but also take a unique name for the class variables, different global variable name and the name of the user program with the same name.
  When the connection does not add extern "C" statement
  Suppose in C ++ header file module A as follows:
1  // module A header moduleA.h 
2  #ifndef MODULE_A_H
 . 3  #define MODULE_A_H
 . 4  int foo ( int X, int Y);
 . 5  #endif

  The reference function module B:

1  // module B implementation file moduleB.cpp 
2 #include " moduleA.h " 
. 3 foo ( 2 , . 3 );

  In fact, during the connection phase, the connector will find such a file from the target symbol _foo_int_int moduleA.obj module A generated!

  After adding extern "C" statement to compile and connections
  After adding extern "C" declaration header file module A becomes:
1  // module A header moduleA.h 
2  #ifndef MODULE_A_H
 . 3  #define MODULE_A_H
 . 4  extern  " C "  int foo ( int X, int Y);
 . 5  #endif

  Still call foo (2,3) in the implementation file module B, the result is:

  (1) A module foo compiled object code, without the special treatment of its name, using the C language mode;
  (2) when the connector module is looking foo object code B (2,3) call, looking for the symbolic name of the unmodified _foo.
  If declared in the module A function foo as extern "C" type, the module B contains the extern int foo (int x, int y), B function module A is not found module; and vice versa.
  So, the real purpose can be summarized in one sentence extern "C" of this statement (the birth of any grammatical characteristics of any language are not arbitrary and are derived from real-world demand-driven. When we think, can not just stop in this language is how to do, but also ask why it should do so, what is the motive, we can a deeper understanding of many issues):
  Implement C ++ mixed programming with C and other languages.
Understand in C ++ extern "C" of the establishment of motivation, specific analysis extern "C" usual tips below us.
 
    4.extern "C" idiom
   (1) reference functions and variables in the C language C ++, when the C language header files comprising (assumed cExample.h), required for the following process:
extern "C"
{
#include "cExample.h"
}
  In the header files in the C language, its external function can only be specified as extern type, C language does not support extern "C" declaration, included in the .c file compiler syntax error occurs when the extern "C".
  I quote written in C ++ source code files of three C function examples of projects include the following:
. 1  / * C language header files: cExample.h * / 
2  #ifndef C_EXAMPLE_H
 . 3  #define C_EXAMPLE_H
 . 4  extern  int the Add ( int X, int Y);      // NOTE: written extern "C" int add (int , int); may be 
. 5  #endif 
. 6  / * C language file: cExample.c * / 
. 7 #include " cExample.h " 
. 8  int the Add ( int X, int Y)
 . 9  {
 10   return X + Y;
 . 11  }
 12 is // c ++ implementation file, call the Add: cppFile.cpp 
13  extern  " C " 
14  {
 15   #include " cExample.h "         // NOTE: wrong here, if this compilation pass, and replaced with extern "C" int add (int, int); by 
16  }
 . 17  int main ( int argc, char * the argv [])
 18 is  {
 . 19   the Add ( 2 , . 3 );
 20 is   return  0 ;
 21 is }

  If C ++ calling .DLL written in a C language header file when including .DLL or declare interface functions, they should add extern "C" {}.

   (2) references to functions and variables C ++ language in C, C ++ header files need to add extern "C", but not directly refer declared extern "C" of the header files in the C language, it should only be C extern file in the C ++ defined "C" function declared as extern type.
  I quote written in C source code files of three C ++ functions example project included the following:
. 1  // C ++ header files cppExample.h 
2  #ifndef CPP_EXAMPLE_H
 . 3  #define CPP_EXAMPLE_H
 . 4  extern  " C "  int the Add ( int X, int Y);
 . 5  #endif 
. 6  // C ++ implementation file cppExample.cpp 
. 7 #include " cppExample. H " 
. 8  int the Add ( int X, int Y)
 . 9  {
 10   return X + Y;
 . 11  }
 12 is  / * C implementation file cFile.c
13  / * This will compile error: #include "cExample.h" * / 
14  extern  int the Add ( int X, int Y);
 15  int main ( int argc, char * the argv [])
 16  {
 . 17   the Add ( 2 , . 3 );
 18 is   return  0 ;
 . 19 }

  If the in-depth understanding of Section 3 as set forth in the role of extern "C" play in compiling and linking stage, we can truly understand the idiom references C and C functions from C ++ references to the C ++ function set forth in this section. Sample code given in Section 4, all the details require special attention.

 

 

Reproduced in: https: //www.cnblogs.com/Jerryli/p/4315340.html

Guess you like

Origin blog.csdn.net/weixin_33859844/article/details/94158420