extern "C"--using "C" to compile and link in C + +

Source: Internet
Author: User

An explanation of the extern "C" usage in C + +

extern "C" indicates a compilation protocol in which extern is the keyword attribute and "C" characterizes the compiler link specification. For the extern "C" can be understood as a mixed programming in the C++/C of the compilation instructions. Use "C" to compile and link in C + +.

extern "C" contains a double meaning, literally: First, the object it modifies is "extern", and secondly, the target it modifies is "C". Let's take a detailed reading of this twofold meaning.

1. A function or variable defined by extern "C" is of type extern;

2. Extern is a keyword that indicates the scope (visibility) of functions and global variables in the C + + language, which tells the compiler that its declared functions and variables are an external variable or function that has been defined, and that, with this declaration, it is possible to use the variable or function legitimately from the point of declaration . for an extern variable, it is simply a declaration of a variable, not the allocation of memory space in the definition. Remember, the following statement:extern int A; Just a declaration of a variable, which is not defined in variable A, does not allocate a memory space for a , and the type name int can be omitted from writing . variable A can only be defined once in all modules as a global variable, or a connection error occurs.

In general, you can also use the keyword extern declaration for functions and global variables referenced by This module a to other modules in the header file of module A. 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. and the compilation phase, outside the function can not be found , but do not error. The link stage finds this function from the target code that defines the module's build.

The keyword that corresponds to extern is static, and the global variables and functions it modifies can only be used in this module.

As an object-oriented language, C + + supports function overloading, whereas programming language C is not supported. From the code to the executable program needs to be compiled and linked two procedures, in which the compilation phase will do syntax detection, code expansion, in addition it will do one thing, is to change the variable into a symbol , the link is actually through the symbol to locate. When the compiler compiles C and C + + code, the process symbol names that convert the variables to symbols are different, and mixed programming occurs when the function is not found in the link. 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 "Mangledname"). _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.

It is also easy to understand why C + + supports function overloading and C does not support it, because C + + adds the function's parameter type to the symbol when C + + does not, so in C + +, even if the function name is the same, their symbolic names are not conflicting as long as the parameters are different .

(1) in C + + to reference the function in the language, you need to add extern "C" in C + + header file, otherwise C + + compile will not find the symbol

/* C Language header file: CExample.h */

1234 #ifndef C_EXAMPLE_H#define C_EXAMPLE_Hexternint add(int x,inty);    //对其外部函数指定为extern#endif

/* C language Implementation file: CEXAMPLE.C */

12345 #include "cExample.h"intadd( int x, int y ){   returnx + y;}

C + + implementation file, call Add:cppFile.cpp

123456789 extern"C"  //采用C编译器编译的C语言代码段{   #include "cExample.c" }int main(int argc, char* argv[]){   add(2,3);   return0;}

(2) in C refers to a function in the C + + language, C + + header file needs to add the extern "C", but in the C language can not directly reference the declaration of the extern "C" of the header file, you should only c files in the definition of C + + the extern "C" function is declared as extern type.

1. Use extern "C" {} to declare functions that will be used by C programs in the. h file of C + +

C + + header file cppExample.h

1234 #ifndef CPP_EXAMPLE_H#define CPP_EXAMPLE_Hextern"C" int add( int x, inty );  //采用C编译器编译的C语言代码段#endif

2. Implement the above functions in a. cpp file in C + +

C + + implementation file CppExample.cpp

12345 #include "cppExample.h"intadd( int x, int y ){    returnx + y;}

3. Use extern to declare C + + functions to use in. c Files

c Implement file cfile.c Note: You must not include the C + +. h file in the. c file, so the compilation fails

/* This compiles an error: #include "cppExample.h" */

12 3456 extern  int  add (  int   x,  int  y);     //only declares the extern "C" function defined in C + + as the extern type main (  int   ARGC,  char * argv[]) { add (2, 3); return  0; }

extern "C"--using "C" to compile and link in C + +

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.