About # ifdef _ cplusplus extern

Source: Internet
Author: User

The following code is often seen in the CPP Code:

# Ifdef _ cplusplus
Extern "C "{
# Endif

// A piece of code

# Ifdef _ cplusplus
}
# 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. 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 used out of 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
# 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" to describe.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nih1986517/archive/2008/10/12/3065386.aspx

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.