extern "C" parsing

Source: Internet
Author: User

The analysis of turning arrogant cow (very detailed) http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html

I make a simple annotation to see for myself later:

In the C + + project source code, it is often unavoidable to see the following codes:

#ifdef __cplusplus  //in C + +, automatically define __cplusplus's macro extern"C"  {  #endif/*... */ #ifdef __cplusplus} #endif

extern "C" is for C + + and C and other languages of the mixed programming, because C + + support function overloading, the same name function in the compilation phase according to the parameter feature table different names of different global name, such as:

C + + void func (int,int) named _func_int_int

and C-mode is named _func after compiling.

3.2, c compilation and connection

The C language does not overload and class these features, it is not as C + + as print (int i), will be compiled to _print_int, but directly compiled into _print and so on. So if a function that calls C directly in C + + fails, because the connection is called print (3) in C, it will look for _print_int (3). So the role of extern "C" is reflected.

3.3. C code in C + +

Suppose a C header file CHeader.h contains a function print (int i), in order to be able to invoke it in C + +, the extern keyword must be added (the reason is described in the extern keyword section). Its code is as follows:

1 #ifndef C_header 2 #define C_header3  4externvoid print (int  i); 5  6 #endif C_header

The corresponding implementation file for the CHEADER.C code is:

1 #include <stdio.h>2"cHeader.h"3void print ( int i) 4 {5     printf ("cheader%d\n", i); 6 }

The print (int i) function in C is now referenced in C + + code file C++.cpp:

1 extern "C"{//To tell the compiler that this is compiled and linked in C language, call here the function is also in C to do2#include"CHeader.h"3 }4  5 intMainintargcChar**argv)6 {7Print3);8     return 0;9}

Execute program output:

3.4. C calls C + + code

Now replace the code that calls C + + in C, which is different from the code that calls C. The following code is defined in the CppHeader.h header file as follows:

1 #ifndef Cpp_header 2 #define Cpp_header3  4extern"C"void print (  int  i); 5  6 #endif Cpp_header

The code in the corresponding implementation file CppHeader.cpp file is as follows:

 1  #include  " cppheader.h   " 2  4  using  namespace   STD;  5  void  print ( int   i)  6  7  cout<< cppheader   " <<i<<ENDL;  8 } 

Call the print function in C's code file C.C:

extern void print (int  i); int Main (int argc,Char* * argv) {    print (3)    ; return 0 ;}

Note The code file in C directly include the "CppHeader.h" header file, and the compilation error. And if you do not add extern int print (int i), the compilation will also be an error.

extern "C" directives are useful because of the close relatives of C and C + +. Note: c, in the extern "C" directive, represents a compilation and connection specification, not a language. C represents any language that complies with the C language's compilation and connection protocols, such as FORTRAN, assembler, etc.

Also note that the extern "C" directive specifies only compilation and connection specifications, but does not affect semantics. For example, in a function declaration, extern "C" is specified, and the C + + type detection and parameter conversion rules are still adhered to.

Looking at the following example, in order to declare a variable instead of defining a variable, you must specify the extern keyword when declaring it, but when you add "C", it will not change semantics, but it will change the way it compiles and joins.

If you have a lot of languages to add extern "C", you can put them in extern "C" {}.

extern "C" 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.