Function of extern & quot; c & quot;

Source: Internet
Author: User

The role of extern "c" and the role of extern

Function: Mixed Programming of C and C ++.

Principle: After the C and C ++ compilers compile, the function name will be compiled into different names. The target cannot be found in the Link stage name search, which will be explained in the following examples.

Usage: ①. Function defined in file c ,. when the cpp file is to be called. use extern "C" to declare the function in the cpp file; ② reverse ,. functions defined in the cpp file ,. to call the c file. use extern "C" to declare in the cpp file ,. the c file only uses the normal extern declaration.

The two source files are compiled into. o files respectively, and then linked to the execution file. You must use g ++ to generate an execution file by linking the. o file.

 

Instance:

Declared in the source file,No HeaderFile

Function defined in the. c file, called in the. cpp file:

1 //. function 2 extern int myadd (int a, int B) defined in file c; 3 4 int myadd (int a, int B) 5 {6 return a + B; 7}
1 //. cpp file call 2 # include <iostream> 3 using namespace std; 4 5 extern "C" int myadd (int a, int B); 6 7 int main () 8 {9 cout <myadd (3, 7) <endl; 10 return 0; 11}

 

Function defined in the. cpp file, called in the. c file:

1 //. the cpp file defines the function 2 extern "C" int myadd (int a, int B); 3 4 int myadd (int a, int B) 5 {6 return a + B; 7}
1 //. c file call 2 # include <stdio. h> 3 4 extern int myadd (int a, int B); 5 6 int main () 7 {8 printf ("% d \ n", myadd (3, 7); 9 return 0; 10}

 

IfDeclare in header file, The. c file normally contains this header file, and the. cpp file should be used

1 //. cpp File Inclusion Method 2 extern "C" 3 {4 # include "myHead. h" 5}

To include the header file. All the functions declared in this header file are compiled in C mode.

 

Principle supplement: In the C file, the name of the previously compiled function is similar to _ myadd. After CPP is compiled, the name of the function is similar to _ myadd_int_int, because CPP must support overloading, this method is used to distinguish between overloaded functions.

 


What is extern "c"

Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C. Let's explain these two meanings in detail.
(1) The function or variable specified by extern "C" is of the extern type.
Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statements:
Extern int;
It is just a declaration of a variable. It does not define variable a and does not allocate memory space for variable. Variable a can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.
In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls A function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function from the target Code Compiled by module A in the connection phase.
The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C.
(2) variables and functions modified by extern "C" are compiled and connected in C language.

In C language, what is the role of extern?

When defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function: [extern] nbsp; function type nbsp; function Name (function parameter table ){......} When calling an external function, you need to describe it: [extern] nbsp; function type nbsp; function name (parameter type table )[, function name 2 (parameter type table 2) ......]; [Case] external function application. (1) file mainf. cmain () {nbsp; externnbsp; voidnbsp; input (...), Process (...), Output (...); Input (...); Nbsp; process (...); Nbsp; output (...);} (2) file subf1.c ...... Externnbsp; nbsp; voidnbsp; input (......) Nbsp; nbsp;/* define external functions */{......} (3) file subf2.c ...... Externnbsp; nbsp; voidnbsp; process (......) Nbsp; nbsp;/* define external nbsp; function */{......} (4) file subf3.c ...... Externnbsp; voidnbsp; output (......) Nbsp; nbsp;/* defines external functions */{......}

Related Article

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.