extern "C" usage

Source: Internet
Author: User

In Windows Driver development, if you are using C + + development, then you must be in some key function money plus extern c keyword, otherwise compiled function, and C language compiled function is different, cause the driver can not

be effectively identified.

The most critical is the DriverEntry function, if the use of C + + developed CPP, then, the compiler compiled function will be _driverentry, when it is actually compiled with c is [email protected] a function rule with return value after ... Therefore, you must declare this function to declare the compilation according to the C Speech compilation rules:

Introduction

C + + retains some of the characteristics of a procedural language, 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.
The main purpose of extern "C" is to be able to correctly implement C + + code calls to other C language code. The addition of extern "C" instructs the compiler to compile this part of the code in C instead of C + +. Because C + + supports function overloading, the compiler compiles the function by adding the parameter type of the function to the compiled code, not just the function name, and the C language does not support the functions overloading, so the function of compiling C code does not take the parameter type of the function, generally including the function name.
For example, you have developed a DLL library with C, in order to enable the C + + language to call your DLL output (export) function, you need to use extern "C" to force the compiler not to modify your function name.

The secret extern "C" starts with the standard header file.
#ifndef __incvxworksh /* Prevent the header file from being repeatedly referenced */#define __incvxworksh#ifdef __cplusplus //__ Cplusplus is a custom macro in CPP extern  "C" { //tells the compiler that this part of the code compiles in the C language format instead of the C + + # endif /**** some declaration or so *****/#ifdef __cplusplus}#endif#endif /* __incvxworksh */          
The meaning of extern "C"

extern "C" contains a double meaning, literally: First, the object it modifies is "extern", and secondly, the target it modifies is "C".
The function or variable defined by extern "C" is of type extern;
1. extern key word
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.
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 link stage from the target code generated by module a compilation.
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.

2. Variables and functions modified by extern "C" are compiled and linked in C language.
Let's start with a look at how C + + is compiled for a function that is similar to.
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.

3. Examples and explanations
(1) Connection method without extern "C" declaration
Assume that in C + +, the header file for module A is as follows:

// 模块A头文件 moduleA.h#ifndef MODULE_A_H#define MODULE_A_Hint foo( int x, int y );#endif
//在模块B中引用该函数:// 模块B实现文件 moduleB.cpp#include "moduleA.h"foo(2,3);

In fact, during the connection phase, the linker looks for symbols such as _foo_int_int from the target file Modulea.obj generated by module A!

(2) Add extern "C" after the declaration of the compile and link mode
After adding the extern "C" declaration, the header file of module a becomes:

// 模块A头文件 moduleA.h#ifndef MODULE_A_H#define MODULE_A_Hextern "C" int foo( int x, int y );#endif

Foo (2,3) is still called in the implementation file of Module B, and the result is:

<1>a compile to generate the target code of Foo, the name is not special processing, the use of C language method;

<2> the linker is looking for an unmodified symbol name _foo when looking for foo (2,3) calls for the target code of Module B.

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.

The true purpose of the extern "C" statement is to achieve mixed programming of C + + and C and other languages .

Application situations
    • C + + code calls the code, and uses it in the header file of C + +
      In C + +, references to functions and variables in the C language require the following processing when the C language header file is included (assuming cExample.h):
      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".
/* C Language header file: CExample.h */#Ifndef C_example_h#Define C_example_hexternIntAdd(int x,int y);Note: written as extern "C" int add (int, int); can also#endif/* C language Implementation file: CEXAMPLE.C */#Include"CExample.h"int add ( int x, int y) {return x + y;} //C + + implementation file, call Add:cppfile.cppextern  "C" {#include " cExample.h "//Note: Here is not appropriate, if this compilation pass, instead of the extern " C "int add (int, int); can be}int main ( Span class= "Hljs-keyword" >int argc, char* argv[]) {Add (2,
                                           
                                            3); 
                                            return 0;}   
                                                 

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.

    • When referencing functions and variables in the C + + language, the header file of C + + needs to be added extern "C", but the header file that declares extern "C" cannot be directly referenced in C, it should only be declared as extern "C" functions defined in C + +
C + + header file CppExample.h#Ifndef Cpp_example_h#Define Cpp_example_hexternCIntAdd(int x,int y);#endifC + + implementation file CppExample.cpp#Include"CppExample.h"int add ( int x, int y) {return x + y;} /* C implementation file cfile.c/* This compiles an error: #include "cExample.h" */extern int add  (int x, int y); int main (int argc, char* argv[]) {Add (2, 
                                             
                                              3); 
                                              return 0;}   
                                                   

extern "C" usage

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.