Download the source code of the example of this article into C + + code, no style conversion, do not refactor, just for the code can pass through C + + compiler. 1. Global functions and global variable declarations and invocations. When you change to C + + code, the C + + compiler does not have the same decorated name (name mangle) as the C compiler for the function, so in the mutual invocation of the C and C + + functions, a link error occurs in the use of the global variable and in the reference to the header file. The precompiled directive extern "C" comes in handy, and it is the only precompiled instruction that all C + + compilers can guarantee. (1) In order to be able to be used in other CPP, functions and global variables that were defined with C may be defined in this way://to is called in. cpp file. #ifdef __cplusplus extern "C" {#endif extern int G_my_var int Add_c (int x, int y); int sub_c (int x, int y); & nbsp #ifdef __cplusplus} #endif now converted to C + + code, you need to modify this: extern int g_my_var; int Add_c (int x, int y); int Sub_c (int x, int y); if you want C + + functions and variables to still be used by C files, you have to remove the function decorated name of C + +, still use the function decorated name C, you need to precede the extern "C" keyword: extern "c" int sub_cpp (int x, int y); /may is called in C function. (2) The original function of calling other CPP files in this C file has become C + + called C + + without any modification. (3) The function of calling other C files in this C file function and the global variables using C are now converted to C + +. Need to modify: A. If the header file that contains the function has a precompiled statement defined by (1), use only the header file. B. If not, change to: extern "C" {#include"C.h"} or #include "C.h" extern "C" {int add_c (int x, int y); int Sub_c (int x, int y); extern "C" int g_my_var; (4) originally in other C files to call the functions in this C file, the function of the other C file is lost in C + +. Declare the function used in the call to C file and make sure that the extern "C" modifier is at the declaration of the header file where the function is located. (5) Originally in other CPP file call this C file function, now become other CPP file function call C + +, then in the call to remove precompiled directive extern "C", directly include header file. extern "C"//{#include "C.h"//} or #include "C.h" //extern "C"//{//int (int x, int y); int Sub_c (int x, int y); extern int G_my_var; 2. Type of transformation. 3. The assignment between function pointers (same as 2, need to focus on, especially as a separate). The C + + compiler checks the type more strictly than the C compiler. The following code C compiler passes but the C + + compiler cannot pass. int Function (int x, int y); int (*callback) = Function; You need to convert the int Function (int x, int y) as follows; typedef int (*function_callback_type) (int, int); Function_callback_type callBack = reinterpret_cast<function_callback_type> (Function); 4. Variable name modification, in c the name of the custom variable cannot and C + + reserved Words and keywordsSame, like new, this and so on. only to ensure that the code is correctly compiled and linked to minimal changes on the C + + compiler.
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.