Recommendation 19: understand how to use C in C ++

Source: Internet
Author: User

Chapter 1 changes from C to C ++

The father of C ++ originally designed the language as "a better C". Therefore, C ++ is generally considered as a super set of C, but should not be mistaken for it, "This means that C ++ is compatible with everything in C language ". As a language compatible with C, C ++ retains the features of some procedural languages, and most of the C code can be compiled in C ++ easily, however, there are still a few differences, resulting in some effective C Code cannot be compiled in C ++.

Therefore, from C to C ++, we need to make some changes due to these differences, and we should be familiar with these differences, use the rich original C library to better serve the current C ++ project.

Recommendation 19: understand how to use C in C ++

First, analyze the following code snippet:

 
 
  1. // Demo.h  
  2. #ifndef SRC_DEMO_H  
  3. #define SRC_DEMO_H  
  4. extern "C"  
  5. {  
  6. ...  // do something  
  7. }  
  8. #endif  // SRC_DEMO_H 

Obviously, the compilation macro "# ifndef SRC_DEMO_H, # define SRC_DEMO_H, and # endif" in the header file is used to prevent the header file from being repeatedly referenced (see recommendation 9 for details ). So what are the special functions of extern "C? Keep this question for the moment.

C ++ is called "C with classes", "a better C", or "a super set of C", but it is not compatible with C language, the "Datong" between the two cannot completely erase the "slight differences ". The most common difference is that C allows implicit conversion from a void pointer to another type of pointer, but C ++ explicitly prohibits this behavior for security considerations. For example, the following code is valid in C:

 
 
  1. // Implicitly convert from void * to double *
  2. Double * pDouble = malloc (nCount * sizeof (double ));

However, to make it run correctly in C ++, explicit conversion is required:

 
 
  1. double *pDouble = (double *)malloc(nCount * sizeof(double)); 

In addition, there are some other portable problems, such as new and class are keywords in C ++, but in C, they can be used as variable names.

To use a large number of ready-made C libraries in C ++, you must put them in extern "C" {/* code. At this point, you may be able to understand the real functions of the Macros in the code snippets listed in this suggestion. Of course, readers with strong curiosity may have a new question: why is it easy to add extern "C" {/* code? This is a problem. Next we will analyze the real reasons behind this phenomenon: C and C ++ have different compilation and link methods. The C compiler does not include the type information of the function when compiling the function, but only the name of the function symbol. The C ++ compiler carries the type information of the function during compilation to implement function overloading. Assume that the prototype of a function is:

 
 
  1. int Function(int a, float b); 

The C compiler compiles the Function into a symbol similar to _ Function (this symbol is generally called a mangled name). Once the C linker finds this symbol, it can be connected successfully and called. The C-compiled linker does not verify its parameter type information, but assumes that the information is correct, which is the disadvantage of the C-compiled linker. In C ++, the compiler checks the parameter type information, the above function prototype will be compiled into symbols such as _ Function_int_float (this mechanism also provides necessary support for the implementation of function overloading ). During the connection process, the linker searches _
Function_int_float.

Resolving these conflicts is the most direct cause and motivation for setting the extern "C" syntax. The Function of extern "C" is to tell the C ++ linker to look for the symbol used to call the Function, and let the compiler look for _ Function instead of _ Function_int_float.

To implement the code that calls C in C ++, you can use the following methods:

(1) modify the header file of C code. When it contains C ++ code, add extern "C" to the Declaration ". The Code is as follows:

 
 
  1. /* C header file: CDemo. h */
  2. # Ifndef C_SRC_DEMO_H
  3. # Define C_SRC_DEMO_H
  4. Extern "C" int Function (int x, int y );
  5. # Endif // C_SRC_DEMO_H
  6.  
  7. /* C language implementation file: CDemo. c */
  8. # Include "CDemo. h"
  9. Int Function (int x, int y)
  10. {
  11. ... // Processing code
  12. }
  13.  
  14. // C ++ calls the file
  15. # Include "CDemo. h"
  16. Int main ()
  17. {
  18. Function (2, 3 );
  19. Return 0;
  20. }

(2) re-declare the C function in the C ++ code, and add extern "C" to the Declaration ". The Code is as follows:

 
 
  1. /* C header file: CDemo. h */
  2. # Ifndef C_SRC_DEMO_H
  3. # Define C_SRC_DEMO_H
  4. Extern int Function (int x, int y );
  5. # Endif // C_SRC_DEMO_H
  6.  
  7. /* C language implementation file: CDemo. c */
  8. # Include "CDemo. h"
  9. Int Function (int x, int y)
  10. {
  11. ... // Processing code
  12. }
  13.  
  14. // C ++ calls the file
  15. # Include "CDemo. h"
  16. Extern "C" int Function (int x, int y );
  17.  
  18. Int main ()
  19. {
  20. Function (2, 3 );
  21. Return 0;
  22. }

(3) When the C header file is included, add extern "C ". The Code is as follows:

 
 
  1. /* C header file: CDemo. h */
  2. # Ifndef C_SRC_DEMO_H
  3. # Define C_SRC_DEMO_H
  4. Extern int Function (int x, int y );
  5. # Endif // C_SRC_DEMO_H
  6.  
  7. /* C language implementation file: CDemo. c */
  8. # Include "CDemo. h"
  9. Int Function (int x, int y)
  10. {
  11. ... // Processing code
  12. }
  13.  
  14. // C ++ calls the file
  15. Extern "C "{
  16. # Include "CDemo. h"
  17. }
  18.  
  19. Int main ()
  20. {
  21. Function (2, 3 );
  22. Return 0;
  23. }

In use, remember: extern "C" must be added to the C ++ code file to work.

Remember:

If you want to use a large number of ready-made C libraries in C ++ to implement mixed programming between C ++ and C, you must understand what extern "C" is like, understand how to use extern "C.

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.