# Ifdef _ cplusplus

Source: Internet
Author: User

The following code is often seen in the CPP Code:

1 # ifdef _ cplusplus
2 extern "C" {// Why is there a # endif in the brackets first and # ifdef _ cplusplus in the end ???
3 # endif // 1st rows correspond to 3rd rows, and 5th rows correspond to 7th rows

4 // a piece of code // {} indicates that all the values in the brackets are compatible with the C functions. If there are no parentheses, extern C only modifies the following sentence.

5 # ifdef _ cplusplus
6}
7 # endif
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 to say, the above Code indicates: if this is a piece of CPP Code, add extern "C" {And} to process the code.

To understand why extern "C" is used, you must start with the overload processing of functions in CPP. In C ++, 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. in C, it is just a simple function name and no other information is added. that is to say, C ++ and C process the name of the generated function differently.

For example, in the following simple function, let's look at the changes in the assembly code generated by adding or not adding extern "C:

Int F (void)
{
Return 1;
}
The assembly code generated when you add extern "C" is:

. 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
But after extern "C" is not added

. 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
The two pieces of assembly code are also generated using the GCC-s command. All parts are the same, except the generated function name. One is _ F and the other is _ z1fv.

We understand the impact of adding and not adding extern "c" to function names. Let's continue our discussion: Why do we need to use extern "C? When designing C ++, the father of C ++ considered that a large number of C Code existed at that time. In order to support the original C code and write the C library, to support C as much as possible in C ++, extern "C" is one of the strategies.

Think about this situation: a library file has been written in C and runs well. In this case, we need to use this library file, but we need to use C ++ to write this new code. If the Code uses the C ++ method to link the C library file, a link error occurs. let's look at a piece of code: First, we use the C processing method to write a function, that is to say, this function was written in C at the time:

// F1.c
Extern "C"
{
Void F1 ()
{
Return;
}
}
The compilation command is: gcc-C f1.c-O f1.o generates a library file named f1.o. Write another code to call this F1 function:

// Test. cxx
// This extern indicates that 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;
}
Use gcc-C test. cxx-O test. O to generate 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:

Test. O (. Text + 0x1f): Test. cxx: undefine reference to 'f1 ()'
That is to say, when compiling test. in cxx, the compiler uses the C ++ method to process the F1 () function, but actually the Linked Library File uses the C method to process the function, so there will be a link error: Because the linker cannot find the function.

Therefore, to call the library file written in C ++ code, we need to use extern "c" to tell the compiler: This is a library file written in C, please use the C method to link them.

For example, now we have a C library file whose header file is F. h. The generated lib file is F. lib, if we want to use this library file in C ++, We need to write as follows:

Extern "C"
{
# I nclude "F. H"
}
Back to the problem above, if you want to correct the Link error, we need to rewrite test. cxx like this:

Extern "C"
{
Extern void F1 ();
}

Int main ()
{
F1 ();

Return 0;
}
Recompile and the link will pass.

Summary

C and C ++ process functions differently. extern "C" is a means to enable C ++ to call library files written by C. If you want to prompt the compiler to use the C method to process functions, use extern "c" to describe.

Related Keywords:

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.