1.Print error message
If the execution of a program requires that a macro be defined, you can use # error, # warning to print the error (warning) information when the macro is not defined. For example:
# Ifndef _ UNIX __
# Error "This section will only work on UNIX systems"
# Endif
The program can be compiled normally only when the _ UNIX _ macro is defined.
2.Easy debugging
_ File, _ line, and _ function are macros predefined by the compiler. They represent the file name, row number, and function name of the current Code respectively.
You can add the following statement to the Code to track code execution:
If (ERR ){
Printf ("% s (% d)-% s \ n" ,__ file __,__ line __,__ function __);
}
3.C/C ++Hybrid programming
Function int Foo (int A, int B );
In C language, the name of this function in the library after the compiler compilation is _ Foo, in C ++, after the function is compiled, its name in the library is _ foo_int_int (changes made to implement function overloading ). If C ++ needs to use the library function compiled by C, the system will prompt that the function cannot be found because the symbol name does not match. Use extern "C" in C ++ to solve this problem. It indicates that the function to be referenced is compiled by C and should be searched for Symbols according to the naming method of C.
If foo is a C-compiled library and you want to use Foo in C ++, add the following statement, where _ cplusplus is a predefined macro of the C ++ compiler, this file is compiled by the C ++ compiler. If you reference the C library function, you need to add the extern "C ".
# Ifdef _ cplusplus
Extern "C "{
# Endif
Extern int Foo (int A, int B );
# Ifdef _ cplusplus
}
# Endif
4.Variable parameters
# Define debug (format, argS...) fprintf (stderr, format, argS)
# Define debug (format,...) fprintf (stderr, format, _ va_args __)
Or # define debug (format,...) fprintf (stderr, format, ##_ _ va_args __)
The first two have redundant commas. The third macro uses the # Remove unnecessary commas.