More effective C + + clause 34 How to combine C + + and C in a program

Source: Internet
Author: User

1. One of the prerequisites of mixed use of C + + and C is that the compiler produces compatible target files (. lib and. dll, etc.). The so-called "compatibility" refers to the compiler in the "pre-compiler-dependent characteristics" consistent, such as int and double size, parameter stack mechanism, Only on this basis can we discuss the problems of using C + + and C modules together.

2. On the basis of 1, to combine the use of C + + and C modules, the following points need to be noted:

1). Name mangling (name reorganization)

Name mangling is a mechanism used by C + + to support function overloading, which makes certain changes to overloaded function names so that each function has a unique name, and because C does not support function overloading, then name mangling is not required.

If C + + is to invoke a function of the C function library, if name mangling is not suppressed, the function name after name mangling may not match the original function name of the C function library, and therefore the link will fail. The workaround is to use extern C to block name Mangling, like this:

extern " C " void Simulate (int iterations);

In this way, the C + + compiler avoids name mangling for simulate when compiling C + + files, and instead uses the same naming method as C. extern "C" is only valid for the next function, if you want to use extern "C" for multiple functions at the same time, use the brackets :

extern " C " {     void drawLine (int int int int. int y2)    ; void Char bits);     void Simulate (int  iterations);    ...}
View Code

Sometimes the code may be used by C + + or it may be used: when used in C + +, you may need to use extern "C", and for C, you can use the predefined __cplusplus macro C + + to make the file "universal in several languages", like this:

#ifdef __cplusplus extern " C " {#endifvoid drawLine (int int int. int y2); void Char bits); void Simulate (int  iterations);.. #ifdef __cplusplus}#endif
View Code

(Note: If you want to use a library of functions such as C,assembler,fortran,lisp,forth, you also need to suppress name mangling and use extern "C". There is no usage like extern "Fortran" because extern " C "does not say that the function is written in C, but instead indicates that the name mangling is suppressed. )

For specific usage, refer to: http://www.cnblogs.com/luxiaoxun/p/3405374.html

2). Initialization of the Statics

The entrance to the program is not actually main, but rather a special function provided by the compiler (such as mainCRTStartup (void), see C + +: Object initialization related), and one of the main tasks in mainCRTStartup is the global object (including the static Class objects, global objects, objects within the scope of the namespace, and objects within the scope of the file) are initialized because C + + supports dynamic initialization (such as global variable int b=a;) but only static initialization (see C + +: Object initialization correlation) So dynamic initialization of global objects involves dynamic initialization, which should be written in C + +, and c main renamed to Realmain, and then C + + main calling Realmain, like this:

extern " C " // Implement this int realmain (intchar//  C library function to complete the main function function int main (  intchar//  C + + code {return  realmain (argc, argv);}
View Code

3). Dynamic Memory allocation

c Dynamic memory allocation and deallocation use malloc (and its variants) and the free function, which only involves allocating and releasing memory, while C + + uses the new and delete keywords and automatically calls the object's constructor. malloc (and its variants)/free with, new/ Delete is used in conjunction with, not confusing.

(Note: Individuals think that as long as you thoroughly understand the internal mechanism of new and delete, mixed problems are not very large, see effective C + + clause 16)

4). Data structure compatibility

C and C + + are compatible with struct and built-in type variables, because C + + is the same for a struct's memory layout, but if C + + is a struct plus a non-virtual member function, its object is still compatible with C because the struct memory layout does not change, but if you add a virtual function, Because of the presence of VTBL and vptr, the struct memory layout changes and is no longer compatible with C.

That is, it is safe to have two-way communication between C and C + + on the data structure, provided that the definition of the structure can be compiled in C and C + +, and that if the C + + struct is no longer compatible with C, it may not affect compatibility, and other changes such as virtual functions and inheritance will have an impact.

More effective C + + clause 34 How to combine C + + and C in a program

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.