How to use C in C ++
First, analyze the following code snippet:
- // Demo.h
- #ifndef SRC_DEMO_H
- #define SRC_DEMO_H
- extern "C"
- {
- ... // do something
- }
- #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:
- // Implicitly convert from void * to double *
- Double * pDouble = malloc (nCount * sizeof (double ));
However, to make it run correctly in C ++, explicit conversion is required:
- 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:
- 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:
- /* C header file: CDemo. h */
- # Ifndef C_SRC_DEMO_H
- # Define C_SRC_DEMO_H
- Extern "C" int Function (int x, int y );
- # Endif // C_SRC_DEMO_H
-
- /* C language implementation file: CDemo. c */
- # Include "CDemo. h"
- Int Function (int x, int y)
- {
- ... // Processing code
- }
-
- // C ++ calls the file
- # Include "CDemo. h"
- Int main ()
- {
- Function (2, 3 );
- Return 0;
- }
(2) re-declare the C function in the C ++ code, and add extern "C" to the Declaration ". The Code is as follows:
- /* C header file: CDemo. h */
- # Ifndef C_SRC_DEMO_H
- # Define C_SRC_DEMO_H
- Extern int Function (int x, int y );
- # Endif // C_SRC_DEMO_H
-
- /* C language implementation file: CDemo. c */
- # Include "CDemo. h"
- Int Function (int x, int y)
- {
- ... // Processing code
- }
-
- // C ++ calls the file
- # Include "CDemo. h"
- Extern "C" int Function (int x, int y );
-
- Int main ()
- {
- Function (2, 3 );
- Return 0;
- }
(3) When the C header file is included, add extern "C ". The Code is as follows:
- /* C header file: CDemo. h */
- # Ifndef C_SRC_DEMO_H
- # Define C_SRC_DEMO_H
- Extern int Function (int x, int y );
- # Endif // C_SRC_DEMO_H
-
- /* C language implementation file: CDemo. c */
- # Include "CDemo. h"
- Int Function (int x, int y)
- {
- ... // Processing code
- }
-
- // C ++ calls the file
- Extern "C "{
- # Include "CDemo. h"
- }
-
- Int main ()
- {
- Function (2, 3 );
- Return 0;
- }
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.