In the process of writing code, often output some debugging information to the screen, will normally call printf this kind of function.
But when debugging is done, we need to manually remove or annotate these places.
Recently watching the "Linux C programming One-stop learning" This book, I think of a method:
Copy Code code as follows:
void myprintf (char* fmt, ...)
{
}
#ifdef DEBUG
#define printf (fmt, args ...) myprintf (FMT, # #args)
#endif
Debug phase with Debug debugging, the official line can turn printf into an empty function.
One potential risk of doing this is that it may cause dictation glib function to call printf output error log also to be canceled.
Thankfully, most glib calls should be fprintf.
Although the problem solved, but I to args ... and # #args还是不太了解. On the internet to find some of the GCC handbook information as follows:
Macros with variable parameters (Macros with a Variable number of Arguments)
In the 1999 version of the ISO C standard, macros can be defined as functions with variable parameters. The syntax of a macro is similar to the syntax of a function.
Here's an example:
Copy Code code as follows:
#define DEBUG (format, ...) fprintf (stderr, format, __va_args__)
Here, ' ... ' means variable parameters. When this type of macro is invoked, it (here refers to ' ... ') is represented as 0 or more symbols, including the comma inside, until the end of the right bracket. When invoked, the collection of symbolic sequences in the macro body (macro) replaces the inside __va_args__ identifier. For more information, refer to the CPP manual.
GCC always supports complex macros, which use a different syntax so that you can give variable parameters a name, just like any other parameter. For example, the following example:
Copy Code code as follows:
#define DEBUG (format, args ...) fprintf (stderr, format, args)
This is exactly the same as the macro example defined in the ISO C above, but it is more readable and easier to describe.
GNU CPP also has two more complex macro extensions that support the format defined in both of the above formats.
In standard C, you can't omit a variable parameter, but you can pass it an empty argument. For example, the following macro call is illegal in ISO C because there is no comma after the string:
Copy Code code as follows:
GNU CPP In this case allows you to completely ignore the variable parameters. In the example above, the compiler still has a problem (complain), because after the macro is expanded, there is an extra comma behind the string.
To solve this problem, CPP uses a special ' # # ' operation.
The writing format is:
Copy Code code as follows:
#define DEBUG (format, ...) fprintf (stderr, format, # # __va_args__)
Here, if the variable parameter is ignored or empty, the ' # # ' operation causes the preprocessor (preprocessor) to remove the comma in front of it. If you do provide some variable parameters when a macro is invoked, the GNU CPP works fine, and it puts these variable parameters behind the comma. Like other pasted macro parameters, these parameters are not extensions of macros.
specific See "Linux C Programming One-stop learning", by the way praise this book, write very well!