Explain the extern "C" in C ++ !!

Source: Internet
Author: User

I always know that using extern "C" in C ++ "{
} C language is called, but I don't know why. Although I also know the naming rules of the compiler, I didn't think of it. I suddenly realized it when I saw this article. However, because I have been using linux
I don't have the experience of using C ++ in C language. I hope it will be available in the future so that it can be integrated.

Key points: function overloading and naming rules

1. After C ++ and C are compiled, function names are different in the assembly language stage:

The following code is often seen in the cpp Code:
De> # ifdef _ cplusplus
Extern
"C
"{
# Endif
// A piece of code
# Ifdef _ cplusplus
}
# Endif de>
What does this code mean? First, __cplusplus is the custom macro in cpp. If this macro is defined, it indicates that this is a piece of cpp Code, that is, the above Code
The meaning is: if this is a piece of cpp Code, addExtern
"C
"{
And}
Location
The code.
Understand why to useExtern
"C
", We have to start with the overload processing of functions in cpp.
InC
++, In order to support the overload mechanism, some processing should be performed on the function name in the compilation code, such as the return type of the function.
InC
Is a simple function name, and no other information is added. That is to say:C
++ AndC
Pair
The name of the generated function is processed differently.
For example, for the following simple function, we can see whether to add or notExtern
"C
"Produced assembly
Code changes:
De> int F (void)
{
Return 1;
} De>
JoinExtern
"C
"The function name is generated in the form of C, and the compiled code is generated.
Yes:
De>. File "test. cxx"
. Text
. Align 2
. Globl _ f

. Def _ F;. SCL 2;. Type 32;. endef
_ F:
Pushl % EBP
Movl % ESP, % EBP
Movl $1, % eax
Popl % EBP
RET de>
But not added.Extern
"C
"Then, function names are generated in the form of C ++.
The assembly code is:
De>. File "test. cxx"
. Text
. Align 2
. Globl _ z1fv

. Def _ z1fv;. SCL 2;. Type 32;. endef
_ Z1fv:
Pushl % EBP
Movl % ESP, % EBP
Movl $1, % eax
Popl % EBP
RET de>
Both Assembly codes use GCC.
The-s command is generated in the same place, except for the name of the generated function. One is _ F and the other is _ z1fv. C ++ has the function name overload function, so in the compilation stage, before the function name
Add @ or other characters to ensure the correctness of the reload.

Ii. instance analysis:

Understand whether to join or not to joinExtern
"C
"The effect on the function name will follow us.
Why?Extern
"C
"What?C
++
Father in DesignC
++, Considering that a large numberC
Code, in order to support the originalC
Generation
Code and already writtenC
Library, you needC
++C
,
WhileExtern
"C
"Is one of the policies.
Imagine a situation where a library file is already usedC
After writing and running well, we need to use this library file, but we need to useC
++
To write this new code. If the code isC
++C
Library file, then
There will be a link error. Let's look at a piece of code: First, we useC
To write a function, that is, assume that this function was used at the time.C
Write
Of:
De> // f1.C

Extern
"C
"
{
Void f1 ()
{
Return;
}
} De>
The compilation command is: gcc-C
F1.C
-O f1.o
A library file named f1.o is generated. Write another code to call this f1 function:
De> // test. cxx
// ThisExtern
The f1 function is defined elsewhere.
// Compile, but the link is still required
// Link the original library file.
Extern
Void f1 ();
Int main ()
{
F1 ();
Return 0;
} De>
Through gcc-C
Test. cxx
-O test. o generates a file named test. o. Then, we use gcc test. o f1.o to link two files, but an error occurs. The error prompt is:
De> test. o (. text + 0 × 1f): test. cxx: undefine reference to 'f1 ()'
In f1.o, the f1 () function is compiled according to the c rule. The Assembly name may be _ f,. in cxx, f1 is compiled according to the c ++ rule. The Assembly name may be _ @ ssf1.
Function names generated under different mechanisms are different, so they cannot be connected. De>

That is to say, when compiling test. cxx, the compiler usesC
++ Method to process the f1 () function, but the actual chain
The connected library file is actually usedC
To process the function, so there will be a link error: Because the linker cannot find the function.
Therefore, in orderC
++ Code callC
You must useExtern
"C
"To tell the compiler: This is a useC
UseC
Of
To link them.
For example, now we haveC
Library file. Its header file is f. h, and the generated lib file is f. lib.C
++
To use this library file, we need to write as follows:
De>Extern
"C
"
{
# Include "f. h"
} De>
Back to the problem above, if you want to correct the Link error, we need to rewrite test. cxx like this:
De>Extern
"C
"
{
Extern
Void F1 ();
}
Int main ()
{
F1 ();
Return 0;
} De>
Recompile and the link will pass.
B>
Summary
C
AndC
++ Functions are processed in different ways.Extern
"C
"YesC
++ Can callC
Written Library File
One way, if you want to prompt the compiler to useC
To process the function, you must useExtern
"C
.

Reposted to: http://liuhongdan.com/. thank you for your contribution!

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.