Usage resolution for extern "C" (RPM)

Source: Internet
Author: User

original link: http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html 1. IntroductionThe C + + language was originally created as "A better C", but this does not mean that C + + global variables and functions in C + + are compiled and connected in exactly the same way as C. As a language compatible with C, C + + retains some of the features of the procedural language (known as "not completely object-oriented"), 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. 2. speaking from the standard header fileAn enterprise once gave the following interview question: Why do standard header files have a structure similar to the following?
1 #ifndef __incvxworksh2     #define__incvxworksh3 4 #ifdef __cplusplus5     extern "C" {6     #endif7 8     /*...*/9 Ten #ifdef __cplusplus One     } A     #endif -  -     #endif/* __incvxworksh */
Analysis Obviously, the compiler macro "#ifndef __incvxworksh, #define __incvxworksh, #endif" In the header file is to prevent the header file from being repeatedly referenced. This can be implemented in another way, #pragma once so
1 #ifdef __cplusplus 2 extern " C " {3#endif45#ifdef __cplusplus6}  7#endif
What is the role of it? We will be in the following one by one lanes. 3. Deep Disclosure extern "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. The function or variable defined by extern "C" is of type extern;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. Remember, the following statement: extern int A; is just a declaration of a variable, not a variable A is defined, and a memory space is not allocated for a.  Variable A can only be defined once in all modules as a global variable, or a connection error occurs. 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 target code generated from module a during the connection phase. 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. The variables and functions modified by extern "C" are compiled and concatenated in C language;  How to compile without the extern "C" declaration first look at how C + + is compiled for a function that is similar to c.. 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. Connection method without extern "C" declaration assuming that in C + +, the header file for module A is as follows:
1 // module A header file ModuleA.h 2 #ifndef module_a_h 3 #define Module_a_h4int int int y); 5 #endif
Reference the function in module B:
1 // Module B implementation file ModuleB.cpp 2 " moduleA.h " 3 foo (2,3);
In fact, during the connection phase, the connector looks for symbols like _foo_int_int from the target file Modulea.obj generated by module A! After adding the extern "C" declaration after the compilation and connection method plus the extern "C" declaration, the header file of module a becomes:
 1  //  2   #ifndef Module_a_h  3   #define  Module_a_h4  "  c   int  foo (int  x, int   y);  5   #endif  
Foo (2,3) is still called in the implementation file of Module B, and the result is: (1) When module a compiles the target code of Foo, it does not treat its name in a special way and uses the C language, and (2) when the connector looks for foo (2,3) call for the target code of module B,  is looking for the unmodified symbol name _foo.    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. Therefore, it is possible to summarize the true purpose of the extern "C" statement in one sentence (the birth of any grammatical feature in any language is not arbitrary and is driven by demand from the real world.) When we're thinking about a problem, we can't just stay in the language, ask why it's doing it, what the motivation is, so we can understand a lot more in depth: Implementing a mixed programming of C + + and C and other languages. Understand the C + + in the establishment of the extern "C" motivation, we are below to specifically analyze the extern "C" the usual use of skills.   4.extern "C" the Customary method(1) in C + +, references to functions and variables in the C language, in the case of a C language header file (assuming cExample.h), the following processing is required:
extern "C" {#include "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". The author of C + + reference C Function Example project contains the source code of the three files as follows:
 1  /*   */ 2   #ifndef c_example_h  3   #define  C_ Example_h4  int  Add (int  x,int  y); //  note: written as extern "C" int add (int, int), or  5   #endif  
1 /*  */2"cExample.h"3intint  int  y)4{5return x + y; 6 }  
1 //C + + implementation file, call Add:cppFile.cpp2 extern "C"3 {4#include"cExample.h"        //Note: This is not appropriate, if this compile pass, instead, replace with extern "C" int add (int, int);5 }6 intMainintargcChar*argv[])7 {8Add2,3);9    return 0;Ten}
  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.   (2) when referencing functions and variables in the C + + language, the header file of C + + must be added extern "C", but the header file that declares extern "C" cannot be directly referenced in C, only the extern "C" defined in C + + should be  The function is declared as an extern type. The author of the C-referenced function example of the three files in the project contains the following source code:
 1  //  2   #ifndef Cpp_ Example_h  3   #define  Cpp_example_ H4  extern   " c   " int  Add (int  x, int   y);  5   #endif  
1 // C + + implementation file CppExample.cpp 2 " cppExample.h " 3 int int int y) 4 {5return x + y; 6 }  
1 /*C Implementation file cfile.c2 /* This compiles an error: #include "cExample.h"*/3 extern intAddintXinty);4 intMainintargcChar*argv[])5 {6Add2,3 );7    return 0;8}

An in-depth understanding of the role of the extern "C" in the compile and join phases described in section 3rd will give you a real understanding of the idioms described in this section from C + + to reference C functions and C. For the example code given in section 4th, you need to pay special attention to each detail.

Usage resolution for extern "C" (RPM)

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.