How to use C in C ++

Source: Internet
Author: User

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. 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 for symbols such as _ Function_int_float in the target file generated by the module where the function prototype is located.

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. /* C language implementation file: CDemo. c */
  7. # Include "CDemo. h"
  8. Int Function (int x, int y)
  9. {
  10. ... // Processing code
  11. }
  12. // C ++ calls the file
  13. # Include "CDemo. h"
  14. Int main ()
  15. {
  16. Function (2, 3 );
  17. Return 0;
  18. }

(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. /* C language implementation file: CDemo. c */
  7. # Include "CDemo. h"
  8. Int Function (int x, int y)
  9. {
  10. ... // Processing code
  11. }
  12. // C ++ calls the file
  13. # Include "CDemo. h"
  14. Extern "C" int Function (int x, int y );
  15. Int main ()
  16. {
  17. Function (2, 3 );
  18. Return 0;
  19. }

(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. /* C language implementation file: CDemo. c */
  7. # Include "CDemo. h"
  8. Int Function (int x, int y)
  9. {
  10. ... // Processing code
  11. }
  12. // C ++ calls the file
  13. Extern "C "{
  14. # Include "CDemo. h"
  15. }
  16. Int main ()
  17. {
  18. Function (2, 3 );
  19. Return 0;
  20. }

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.