Mixed use of c and c++

When we write code, we often need to mix C and C++. In order to keep C code and C++ code compatible with the procedure call interface, we need, in order to simplify, to define two macros that we use

1. Add extern "C" as part of the symbol declaration in C++ code.
2. __BEGIN_DECLS  和 __END_DECLS 
In the header file of the C language code, the following code snippets are filled:

  1. __BEGIN_DECLS
  2. .....
  3. .....
  4. __END_DECLS


  1. #if defined(__cplusplus)
  2.        #define __BEGIN_DECLS extern "C" {
  3.        #define __END_DECLS }
  4.        #else
  5.        #define __BEGIN_DECLS
  6.        #define __END_DECLS
  7. #endif

——BEGIN_DECLS

..........

__END_DECLS

Extended compilation is, this part is compiled according to the BEGIN end,


The extended C language is processed uniformly according to the C++ compiler when compiling, so that the C++ code can call the intermediate code generated by C compilation.

Since the header files of the C language may be read by different types of compilers, the header files of the C language must be written carefully.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325475948&siteId=291194637