EXTERN "C"

xiaoxiao2021-03-31  230

1 Introduction

The C language creation is the original intention to "a better C", but this does not mean that the C 's global variables and functions used in the C are identical to the C language identical. As a language that wants to be compatible with C, C retains a part of the process-oriented language (called "uncommon object" by the world "), so it can define global variables and functions that are not part of any class. However, C is an object-oriented programming language. In order to support the overload of the function, C has a significant difference from C's processing of the whole game.

Border = "0" marginwidth = "0" marginheight = "0" src = "http://219.239.88.50:80/adsunion/get/gt/gTP: 8P = 10-pip-software;tp=if;sk=0 ; ck = 0; /? "frameborder =" 0 "NORESIZE =" noreSize "width =" 1 "scrolling =" no "height =" 1 ">

2. Start from the standard header file

A company has given a discourse question:

Interview

Why is the standard header file similar to the following structure?

#ifndef __incvxworksh # define__incvxworksh #ifdef __cplusextern "c" {#ENDIF / *...*/ #ifdef __cplusplus} #endif #ENDIF / * / __INCVXWORKSH * /

analysis

Obviously, the role of the compilation macro "#ifndef __incvxworksh, # define __incvxworksh, # define __incvxworksh," # define __incvxworksh, and # Endif "is to prevent the header file from being repeatedly referenced.

Then

#ifdef __cplusplusextern "c" {#ENDIF #ifDef __cplusplus} #ENDIF

What is the role? We will come in one by one below.

3. Deeply reveal Extern "C"

Extern "C" contains dual meaning, from the literal, first, the target is modified is "extern"; second, the target it is modified is "C". Let us interpret these two meanings in detail.

Functions or variables defined by Extern "C" are extern types;

Extern is a keyword indicating a function and global variable scope (visibility) in the C / C language. This keyword tells the compiler that the function and variables of its declaration can be used in this module or other module. Remember, the following statement:

Extern Int A;

Just a declaration of a variable, it is not in defining the variable A, and the memory space is not allocated. Variable A is only defined as a global variable in all modules, otherwise an incorrect connection error occurs.

Typically, the function and global variables supplied to other modules to other modules in the module's header file are declared in keyword extern. For example, if the module B wants to reference the global variables defined in the module A and the header files containing the module A can be included. Thus, when the function in the module A is called, the module B is not found in the compile stage, but does not report the error, it will find this function from the target code generated from the module A in the connection phase. .

The keyword corresponding to Extern is Static, and the global variables and functions modified by it can only be used in this module. Therefore, a function or variable may only be used by this module, it is impossible to be modified by Extern "C". Variables and functions modified by Extern "C" are compiled and connected according to C language;

Compile mode when adding Extern "C" declaration

First look at how the function similar to C is compiled.

As an object-oriented language, C supports function overload, and process language C does not support. The function is compiled by C and the name in the symbol library is different from the C language. For example, suppose a function of a function is:

Void foo (int x, int y);

This function is compiled by the C compiler to the name _foo, and the C compiler will generate the name like _foo_int_int (the different compiler may be different, but all have the same mechanism. The generated new name is called "mangled name").

_foo_int_int This name contains the function name, the number of function parameters, and the type information, C is relying on this mechanism to implement the function overload. For example, in C , the function void foo (int x, int y) is not the same as the symbol of Void foo (int x, float y), and the latter is _foo_int_float.

Similarly, the variables in C are supported by the local variables, but also support class member variables and global variables. User

The written program class member variable may be the same name with the global variable, we are distinguished by ".". Essentially, the compiler is similar to the process of compile, and also has a unique name for variables in the class, this name is different from the global variable name of the same name in the user program.

Connection method when adding Extern "C" declaration

Assume that in C , the header file of the module A is as follows:

// Module A header file modulea.h # ifndef module_a_h # define module_a_hint foo (int x, int y); # ENDIF

Quote this function in the module B:

// Module B Implement file moduleb.cpp # include "modulea.h" foo (2, 3);

In fact, in the connection phase, the connector finds the symbol of _foo_int_int from the target file generated by the module A!

Compile and connection after adding Extern "C" declaration

After adding the extern "C" declaration, the header file of the module A becomes:

// Module A header file 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 the module B, and the result is:

(1) When the module A compiles the target code to generate the FOO, there is no special treatment for its name, and the C language is used;

(2) When the connector finds the Foo (2, 3) call for the target code for the module B, the connector is looking for unmodified symbolic names _foo.

If the function in the module A declares that the foo is the extern "C" type, the module B is included in the module B, and the module B cannot be found in the module A; vice versa.

Therefore, you can use a sentence to summarize the true purpose of the declaration of Extern "C" (the birth of any language in any language is not random, from the demand drive of the real world. We can't stay when thinking about the problem What is done in this language, but also ask why it is doing this, what is the motivation, so we can understand many problems more deeper): Implement a mixed programming of C and C and other languages.

Understand the setup motivation of Extern "C" in C , we will check the usual use skills of Extern "C" below.

4. 惯 惯 惯 用 法

(1) References the functions and variables in the C language in C , and the following processes are required when including the C language header file (assuming to Cexample.h):

Extern "c" {# incrude "Cexample.h"}

In the header file of the C language, the external function can only be specified as an external function. The EXTERN "C" declaration is not supported in the C language, and the compile syntax error occurs when the .c file contains Extern "C".

The source code of the three files included in the C reference C function example project of the author is as follows:

/ * C language header file: CEXample.h * / # ifndef c_example_h # define c_example_hextern Int add (int x, int y); # Endif / * C language implementation file: Cexample.c * / # include "Cexample.h" int Add (int x, int y) {RETURN X Y;} // C implements file, call Add: cppfile.cpPextern "c" {#include "cexample.h"} int main (int Argc, char * argv [] ) {Add (2, 3); return 0;}

If C is written. During a C language, when including .dll's header file or declared interface functions, EXTERN "C" {} should be added.

(2) When the function and variables in the C language are cited in C, the C header file needs to add Extern "C", but the header file that declares the extern "C" cannot be directly referenced in the C language, it should only be c The extern "C" function defined in C is declared as an extern type.

The source code for the three files included in the C function example project of the author is as follows:

// c header file cppexample.h # i d d p c CPP_EXAMPLE_HEXTERN "C" int Add (int x, int y); # ENDIF // C Implement file cppexample.cpp # include "cppexample.h" int Addd (int X, INT Y) {RETURN X Y;} / * c Implementation File CFILE.C / * This will compile error: #include "Cexample.h" * / Extern Int Add (int X, int y); int Main (int Argc) , char * argv []) {add (2, 3); return 0;}

If you understand the effects of Extern "C" as set forth in Section 3 in Section 3, it can truly understand the customary method of c reference C function and C reference C function as set forth in this section. The sample code given in Section 4 is required to pay special attention to each detail.

Transfer from www.pc-online.com

转载请注明原文地址:https://www.9cbs.com/read-130827.html

New Post(0)