# Ifdef _ cplusplus and extern "C"

Source: Internet
Author: User

Often in CPPCodeSee this code in:

# Ifdef _ cplusplus
Extern "C "{
# Endif

// A piece of code

# Ifdef _ cplusplus
}
# Endif

What does this code mean? First, _ cplusplus is the reserved macro definition of the C ++ compiler. That is to say, the C ++ compiler considers this macro has been defined. 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;

Extern "C "{
# Endif

// A piece of code

}
# Endif // * extern "C "*/

If this is a piece of C code, skip extern "C" {And} and compile:

# Endif // * extern "C "*/

// A piece of code

# Endif //

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, the name of a function must be processed in the compiled assembly code, such as a 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. the purpose is to implement mutual calls between C and C ++.

C. H implementation
# Ifndef _ C_H _
# DEFINE _ C_H _
# Ifdef _ cplusplus
Extern "C "{
# Endif

Void c_fun ();

# Ifdef _ cplusplus
}
# Endif

# Endif
-----------------------------------
C. c implementation
# Include "C. H"
Void c_fun ()
{
}
------------------------------------
Call c_test () in C. C in CPP. cpp ()
CPP. cpp implementation
# Include "C. H"
Int main ()
{
C_fun ()
}
Here, _ cplusplus is the reserved macro definition of the C ++ compiler. That is to say, the C ++ compiler considers this macro has been defined.
So the key is extern "C "{}
Extern "C" indicates that the stuff in the brackets of the C ++ compiler is compiled according to the OBJ file format of C. to connect, follow the naming rules of C.
======================================
So how does C call the cpp_fun () function in C ++?
Because there are C and C ++ first, you can only consider it from the C ++ code.
If a function or variable added to C ++ may be called by a file in C, it should be written in this way. It also uses extern "C "{}
But the header file must be added in the code, because it may be called in C ++.
--------------------------------------
Implementation of CPP. h

# Ifndef _ C_H _
# DEFINE _ C_H _
# Ifdef _ cplusplus
Extern "C "{
# Endif

Void cpp_fun ();

# Ifdef _ cplusplus
}
# Endif

.-------------------------------------------
CPP. cpp implementation
Extern "C" {// tell the C ++ compiler that the extension code is compiled according to the naming rules of C.
Void cpp_fun ()
{
.....
}
 
 
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" for example.

Ming.

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.