[C & CPP] C \ c ++ interaction-extern "C"

Source: Internet
Author: User
   1:  #ifndef __INCvxWorksh
   2:  #define __INCvxWorksh
   3:  #ifdef __cplusplus
   4:  extern "C" {
   5:  #endif
   6:  /*...*/
   7:  #ifdef __cplusplus
   8:  }
   9:  #endif
  10:  #endif /* __INCvxWorksh */

Obviously, the compilation macro "# ifndef _ incvxworksh, # DEFINE _ incvxworksh, and # endif" in the header file is used to prevent the header file from being repeatedly referenced.

What is the role of 3-9 lines of code?

1: Meaning of extern "C"

Extern "C" contains a double meaning. Literally, the target to be modified is "extern". Secondly, the target to be modified is "C. the function or variable specified by extern "C" is of the extern type;

Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules.

Statement: extern int A; it is just a declaration of a variable. It is not defining variable A and does not allocate memory space for variable. Variable A can only be defined once as a global variable in all modules; otherwise, a link error occurs. In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it 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 connection phase.
The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C. Variables and functions modified by extern "C" are compiled and connected in C language.

2: compilation method without the extern "C" declaration
First, let's take a look at how C-like functions are compiled in C ++.
As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is void Foo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library is _ Foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, the new name is "mangled name ").
A name such as _ foo_int_int contains the function name, number of function parameters, and type information. c ++ relies on this mechanism to implement function overloading. For example, in C ++, the void Foo (int x, int y) and void Foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.

Similarly, variables in C ++ support both local variables and class member variables and global variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, the compiler uses a unique name for the variables in the class when compiling, similar to the function processing. This name is different from the global variable name with the same name in the user program.

3: Link Method when the extern "C" statement is not added
Suppose in C ++, the header file of module A is as follows:

1: // module A header file modulea. h
   2:  #ifndef MODULE_A_H
   3:  #define MODULE_A_H
   4:  int foo( int x, int y );
   5:  #endif
   6:   
7: // reference this function in Module B:
   8:   
9: // Module B implementation file moduleb. cpp
  10:  #include "moduleA.h"
  11:  foo(2,3);

In fact, in the Link phase, the linker will find symbols such as _ foo_int_int from the target file modulea. OBJ generated by module.

4: the compilation and link method after the extern "C" statement is added

After the extern "C" statement is added, the header file of module A is changed:

1: // module A header file modulea. h
   2:  #ifndef MODULE_A_H
   3:  #define MODULE_A_H
   4:  extern "C" int foo( int x, int y );
   5:  #endif

In Module B's implementation file, Foo (2, 3) is still called. The result is:
1) When module A compiles and generates the foo target code, it does not perform special processing on its name and uses the C language method;
2) When the connector looks for the Foo (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ Foo.

If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int Foo (INT X, int y ), module B cannot find the function in module A, because the connector will be from modulea. find _ foo_int_int in OBJ, but in modulea. OBJ only has the symbol name_foo, and vice versa.

5. Usage of extern "C"
1) reference functions and variables in C language in C ++

When the C-Language header file (cexample. h) is included, the following processing is required:

   1:  extern "C" {
   2:     #include "cExample.h"
   3:  }

In the header file of the C language, the external function can only be specified as the extern type. The C language does not support the extern "C" declaration. when the c file contains extern "C", a compilation syntax error occurs.

The sample code is as follows:

1:/* C header file: cexample. H */
   2:  #ifndef C_EXAMPLE_H
   3:  #define C_EXAMPLE_H
   4:  extern int add(int x,int y);
   5:  #endif
   6:   
7:/* C implementation file: cexample. C */
   8:  #include "cExample.h"
   9:  int add( int x, int y ) {
  10:  return x + y;
  11:  }
  12:   
13: // C ++ implementation file, call Add: cppfile. CC
  14:  extern "C"  {
  15:  #include "cExample.h"
  16:  }
  17:   
  18:  int main(int argc, char* argv[]) {
  19:      add(2,3); 
  20:      return 0;
  21:  }

If C ++ calls a C-language DLL, when it includes the DLL header file or declared interface function, it should add extern "C "{}.
2). c references functions and variables in C ++

The header file of C ++ needs to be added with extern "C", but the header file declared with extern "C" cannot be directly referenced in the C language, only the extern "C" function defined in C ++ should be declared as the extern type in the C file.

The sample code is as follows:

1: // C ++ header file cppexample. h
   2:  #ifndef CPP_EXAMPLE_H
   3:  #define CPP_EXAMPLE_H
   4:  extern "C" int add( int x, int y );
   5:  #endif
   6:   
7: // C ++ implementation file cppexample. cpp
   8:  #include "cppExample.h"
   9:  int add( int x, int y ) {
  10:      return x + y;
  11:  }
  12:   
13:/* C implementation file cfile. c
14:/* Compilation error: # include "cexample. H "*/
  15:  extern int add( int x, int y );
  16:  int main( int argc, char* argv[] ) {
  17:      add( 2, 3 ); 
  18:      return 0;
  19:  }
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.