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:
- // 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 (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:
- // 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 _
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:
- /* 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.