Talk about extern "C" and talk about extern
Today's class is really boring. I just looked at the role of extern "C". After reading it, I have a little understanding of it. Here I will give it
Let's share with you (the level of chicken in this dish is limited. If something is wrong, I hope you can point it out ).
Extern Keyword:
First, let's take a look at the role of the extern Keyword: the extern keyword can be placed before a variable or function to mark the variable
Or the function definition is in another file, prompting the compiler to find its definition in another module when this variable or function is encountered.
In general, for example, we declare a function in the header file "B. h", and then implement this function in "B. cpp,
When calling the function declared in "B. h" in "main. cpp", you only need to # include "B. h" in "main. cpp ".
. Example:
//b.h#pragma once#include <iostream>using namespace std;void test();
//b.cpp#include "b.h"void test(){ cout << "hello world" << endl;}
//main.cpp#include "b.h"int main(){ test(); system("pause"); return 0;}
In addition to the # include "B. h" method, you can call the function in "B. h". The following method can also be called
B. h.
// Main. cpp
// # Include "B. h "// here, you can do it without comments, but you must comment out the # include <iostream> using namespace std; extern void test (); // tell the compiler the test () function declaration in other files int main () {test (); system ("pause"); return 0 ;}
The above is for the function, so if we define a global variable int x in "B. h" (remember to be a global variable !) Now we
What should I do if I want to access variable x in "main. cpp? Will it be possible to directly access it after include "B. h? Let's try it first.
Below:
//b.h#pragma once#include <iostream>using namespace std;int x = 10;void test();
//main.cpp#include "b.h"int main(){ cout << "x=" << x << endl; system("pause"); return 0;}
We output the variable x in "main. cpp". At this time, there is no problem during compilation, but when it is run, it will report an error: fatal error LNK1169: Find
To one or more symbols with multiple definitions. Using the # include "B. h" method to access variable x is too naive and cannot be accessed, because "B. h"
The access scope of the global variable x is the file scope, which can only be in the "B. h "access this file, remember that it can only be in" B. h. cpp"
# Include "B. h" is not accessible. Is there any other way to access variable x in "main. cpp? Of course,
The "extern" keyword can be used to achieve the goal. The usage is as follows:
// Main. cpp // # include "B. h "// be sure to comment out # include <iostream> using namespace std; extern int x; int main () {cout <" x = "<x <endl; system ("pause"); return 0 ;}
After talking so much nonsense, I finally made the extern keyword clear. (I think I have made it clear.) The next step is today's role.
Extern "C"
To clarify what extern "C" is like, let's talk about the C ++ function overload first. C ++ has function overloading, while C does not
Function Overloading is available. Function overloading means that two or more functions have the same name and different parameter lists. Different parameter lists can be the number of parameters.
Different, or the number of parameters is the same, but the parameter type is different. Note that if the function name is the same, the parameter list is identical but the return value type
Different functions cannot be overloaded. C ++ has function overloading because when the obj intermediate file/target file is generated, the C ++ compiler loads the original function
The combination of name and parameter Information produces a unique internal name. For example, there are two functions void foo (int x) and void foo (void ),
The internal names are _ foo_int and _ foo_void (the actual internal name naming rules should not be like this. Here we do not care about
What is its naming rules? You only need to understand this meaning.) through this way, the compiler can distinguish void foo (int x) and void foo (void)
These two functions. However, this internal name is not generated in the C language. If the C language contains two functions: void foo (int x) and
Void foo (void), when the obj intermediate file/target file is generated, the generated names are the same as _ foo and _ foo.
The interpreter cannot partition two functions, so function Overloading is not involved in the C language.
Because the C ++ compiler and C compiler have different processing methods for function names, when our C ++ program calls C programs or C Programs to call C ++
Program. If the problem extern "C" occurs, the problem must be solved.
So in the end, extern "C" is used to solve the name matching problem and implement mixed programming of C and C ++. Put such a sentence here
It's pale and powerless. Let's give an example.
C ++ calls C-language functions:
//a.h#include <stdio.h>void test(int x, double y);
//a.c#include "a.h"void test(int x, double y){ printf("hello world\n");}
The above test function is implemented in C language. We call it in "main. cpp", first by the following method:
//main.cpp#include "a.h"int main(){ test(1,1.2); getchar(); return 0;}
The above method is used for calling and compilation, but an error is reported during running: fatal error LNK1120: 1 external command that cannot be parsed
The test in main. cpp is not found. To solve this problem, you need to use extern "C", as shown below:
//main.cppextern "C"{#include "a.h"}int main(){ test(1,1.2); getchar(); return 0;}
Of course, the above method will be able to run successfully. The function of extern "C" is to tell the C ++ compiler that you should not consider the content in my "a. h"
The C ++ syntax is used to compile the C language. Of course, you can also use the following method:
//main.cpp#include <iostream>using namespace std;extern "C" void test(int x, double y);int main(){ test(1,1.2); getchar(); return 0;}
C calls the C ++ function:
The above shows how C ++ calls C functions, and how does C call C ++ functions?
//b.h#pragma noce#include <iostream>using namespace std;#ifdef __cplusplus extern "C" {#endif void test();#ifdef __cplusplus }#endif
//b.cpp#include "b.h"void test(){ cout << "hello world" << endl;}
//main.c#include <stdio.h>extern void test();int main(){ test(); getchar(); return 0;}
The above are all the gains of today and we will share them with you.