# Ifdef _ cplusplus

Source: Internet
Author: User

# Ifdef _ cplusplus, which is generally used to Output C ++ code in the form of standard C (called in the form of C ), this is because C ++ is often considered as a superset of C, but the C ++ compiler is still different from the C compiler. It is safe to call the code in C ++ in C. Generally, cross-platform usage is considered as follows: # ifdefined (_ cplusplus) | defined (c_plusplus) // cross-platform definition method extern "C" {# endif //... normal declaration section # ifdefined (_ cplusplus) | defined (c_plusplus)} # the variables and functions modified by endif by extern "C" are compiled and connected in C language; the compilation method without the extern "C" declaration. First, let's take a look at how C-like functions are compiled in C ++. As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is voidfoo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library is _ foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, the new name is called "mangled name "). A name such as _ foo_int_int contains the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading. For example, in C ++, the void foo (int x, int y) and void foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float. During compilation, C ++ will combine function names and parameters to generate an intermediate function name to solve the problem of function polymorphism. C ++ does not, therefore, the corresponding function cannot be found during the link. In this case, the C function needs to use the extern "C" to specify the link. This tells the compiler to keep my name, do not generate an intermediate function name for the link for me. The following code is often seen in the cpp Code: # ifdef _ cplusplus extern "C" {# endif // a piece of code # ifdef _ cplusplus} # What does endif 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" {} 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 to 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 not after extern "C. 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 assembly code is also generated using the gcc-S command, everything is the same, except the name of the generated function. 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: // The f1.c extern "C" {void f1 () {return ;}} 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, so that it can be compiled through //, but the original library file needs to be linked when linking. extern void f1 (); int main () {f1 (); return 0;} passed through gcc-c test. cxx-o test. o generates a file named test. o files. Then, we use gcc test. o f1.o is used to link two files, but an error occurs. The error prompt is: test. o (. text + 0x1f): test. cxx: undefine reference to 'f1 () ', that is, compile 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: extern "C" {# include "f. h "} back to the above problem. To correct the Link error, we need to rewrite test. cxx: extern "C" {extern void f1 () ;}int main () {f1 (); return 0 ;}re-compile and the link will pass. 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.

Related Keywords:
Related Article

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.