# Ifdef _ cplusplus extern "C" {# endif

Source: Internet
Author: User

1. In many programs, we will encounter the following code segment

# Ifdef _ cplusplus
Extern "C "{
# Endif

// C syntax code segment

# Ifdef _ cplusplus
}
# Endif //

First, we should know that __cplusplus is a custom macro in CPP, which indicates that this is a piece of CPP Code. The Compiler compiles the system in C ++ mode .. If we need to use the C language code at this time, we need to add the (extern "C" {) Section to explain, or the compiler will compile the C code in C ++ mode, problems may occur.

This sentence means: This code is the CPP Code __cplusplus is the custom macro in CPP. Therefore, it is compiled in the form of CPP, but the code inside contains the C language code, so we need to explain "extern" C "{" that the compiler uses the C language mode for compilation.

# Ifdef _ cplusplus // The _ cplusplus will be defined in the C ++ compiling environment (plus means "+)
Extern "C" {/tell the compiler that the following functions are c-language functions (because C ++ and C-language are different in function compilation and conversion, mainly because there is heavy load in C ++)
# 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, 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 of adding extern "C" {is to implement mutual calls between C and C ++.

Extern "C" is a linkage declaration. Variables and functions modified by extern "C" are compiled and connected in C language.

1. Example of C ++ calling a function in 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_fun () in c. c In cxx. cpp ()

Implementation of cxx. cpp

# Include "C. H"
Int main ()
{
C_fun ()
}

When C ++ calls a C function, add extern "C" {} to the. h file of C ++ "{}.

So the key is that extern "C" {}, extern "C" tells C ++ that the stuff in the brackets is compiled in the OBJ file format of C, to connect, follow the naming rules of C.

2. c calls 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 it must be added to the Code, the header file must also be added, because it may also be called in C ++.

Implementation of cxx. h

# Ifndef _ C_H _
# DEFINE _ C_H _

# Ifdef _ cplusplus
Extern "C "{
# Endif

Void cpp_fun ();

# Ifdef _ cplusplus
}
# Endif

# Endif

Implementation of cxx. cpp

Extern "C" {// tell the C ++ compiler that the extension code is compiled according to the naming rules of C.
Void cpp_fun ()
{
.....
}
}

C. c implementation

# Include "CPP. H"
Int main ()
{
Cpp_fun ()
}

Conclusion: C and C ++ have different processing methods for functions. 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.

 

 

Instructor song and Hua Qing vision embedded college lecturer

 

# Ifdef _ cplusplus extern "C" {# endif

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.