(c) Macro __cplusplus C + + mixed programming

Source: Internet
Author: User
Tags processing instruction

Some of the previous contents content as a student, rarely used, may be working to meet a little more, but the first comprehensive study still do not want to have something to fall, although not commonly used but at least have an impression

Then go to the third chapter "Macro __cplusplus"

In code written in C and C + +, we often see the following declaration in the header file:

#ifdef __cplusplus extern " C " {#endif    // some code #ifdef __cplusplus} #endif

These types of header files can be compiled in # include to C files, or they can be compiled in C + + files.

Generally used to export C + + code in the form of standard C, is called in the form of C, but the C + + and C compiler is different, the above method makes C calls C + + code is safe.

__cplusplus is a custom macro in CPP that defines this as a piece of CPP code

The meaning of the code is this: If this is a CPP code, then add the extern "C" {and} to handle the code in it.

C + + and C are not the same for the resulting function names, and in compiling the generated assembly code, the names of the functions are processed. (because I have not learned the compilation, the specific principle can look at other people analysis: http://blog.csdn.net/thanklife/article/details/7362893)

because extern "C" can always be C + + on the function name, variable name and other symbols name reorganization, so the compiled C target file and C + + target file variables, function names and other symbols are the same (otherwise not the same, for reasons mentioned in the previous paragraph), The linker can reliably link two types of target files. This approach is referred to as the classic practice of C and C + + mixed header files.

For the above code application, see: http://blog.csdn.net/thanklife/article/details/7362893, as if the front of this blog is a picture, the border is not complete, behind the keyboard knocking or is quite complete, Finally there is an example of the associated C function and the extension invocation of the file.

So, let's look at the following section.

We may think that the __cplusplus macro is only "defined" and "not defined" in both states. In fact, this is not the case, __cplusplus. This macro is usually defined as an integer value. And as the standard changes, the __cplusplus macro will generally be a larger value than the previous table.

For example, in the C++03 standard, the value of __cplusplus is predetermined to 199711L, and in C++11 Standard, the macro __cplusplus is predefined as 201103L. This change can be used for the code.

For example, when a programmer wants to make sure that the code is compiled using a support c++11 compiler, it can be instrumented as follows:

#if __cplusplus < 201103L    #error "should use C++11 implementation"#endif

Here, pre-processing instruction #error is used, which makes code compilation that does not support c++11 immediate error and terminates compilation.

The test results are as follows:

C++11 Standard test:

C++03 Standard test:

It's here, thank you for reading, Happy life ~

(c) Macro __cplusplus C + + mixed programming

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.