First, let’s start with the header file. In many header files, we’ll see statements like this

#ifndef _MYHEADFILE_H
#define _MYHEADFILE_H
/ /... The statement...
#endif // _MYHEADFILE_H
Copy the code

To avoid the same file being included more than once, use #ifndef. If _MYHEADFILE_H is not included, use #define to define a macro _MYHEADFILE_H. #endif ends with #ifndef.

A preprocessor is a small piece of software that processes a C program before it is compiled. Its behavior is controlled by preprocessor instructions, which contain the following:

  1. # define a macro definition
  2. The file contains #include
  3. Conditional compilation #if
#ifdef
#ifndef
#if defined
#if! defined
#elif
#else
#endif
#undef
#error   // Easy to debug, report
Copy the code

Directives start with #. Let’s look at a simple macro definition (object macro)

#defineStandard substitution list
#define3.1415926 PI?
Copy the code

You can rename a type

#define  BOOL  int
Copy the code

Macros can take arguments and are often called macro functions

#defineIdentifiers (x1, X2...) Replace the list
Copy the code

Note that there can be no Spaces between identifiers and (parentheses are required. Let’s look at an example:

#defineMAX(x,y) ((x)>(y)? (x):(y))
#define IS_EVEN(n) ((n)%2==0)
#defineTOUPPER (c) (' a '" (c) & (c) <' z '? (c) - 'a' + 'a' (c))
#define SWAP(T,x,y) {T t=x; x=y; y=t}
Copy the code

We could make it a little more complicated, like writing a macro function that validates a date

#defineISLEAP(y) ((y)%4==0&&(y)%100! =0||(y)%400==0)
#define ISSMALL(m) ((m)==4||(m)==6||(m)==9||(m)==11)
#defineNORMAL(m) (ISSMALL(m)? Speak)
#defineDAYS(y,m) ((m)==2? 28+ISLEAP(y):NORMAL(m))
#define IN(x, from,to) ((x)>=(from)&&(x)<=(to))
#defineVALID (y, m, d) (> 1600 && (y) IN && (m, 1, 12) IN (d, 1, DAYS (y, m)))
Copy the code

Now let’s look at conditional compilation

#if (comdition)
    {// statement ##; }
#endif
Copy the code

If (comdition) is true (logic 1), the following statement is compiled; if (comdition) is false (logic 0), the following statement is not compiled. Examples are as follows:

#define DEBUG
#if DEBUGPrintf (" the Value of I: % d \ n ", I); Printf (" the Value of j: % d \ n ", j);#endif
Copy the code

Format:

#ifConstant expression
Copy the code

When the constant expression is 0, the preprocessor removes the code between #if and #endif. #if will treat the undefined standard character as 0. If no DEBUG is defined, the test for #if DEBUG will fail, but #if! DEBUG succeeds. You can use macros to define file names:

#if define(IA32)
#defineCPU_FILE ia32. "h"
#elif defined(IA64)
# deifine CPU_FILE ia64. "h"
#elif defined(AMD64)
#defineCPU_FILE amd64. "h"
#endif
#include CPU_FILE
Copy the code

You can also cancel defined macros:

#if defined VALUE             // Check if VALUE is defined, if so
#undef VALUE                  // Remove the statement definition
#define VALUE 1000            // Redefine VALUE to 1000
#endif
Copy the code

If the validation is not defined, it can be written like this:

#ifndef VALUE               // If VALUE is not defined
#define VALUE 1000          // Define VALUE as 1000
#endif
Copy the code

Of the macros used above:

  • #undef to undefine;
  • #ifndef is short for if not defined. It can also be written as #if! Defined is if there is no definition;
  • #ifdef is short for if defined, which can also be written as #if defined to check if it’s defined;
  • The difference between #ifdef and #if defined, #ifndef and #if! Defined is similar in that the latter can form complex precompilation conditions, while the former only determines whether a single macro is defined,Such as:
#if defined(PERL_PACK_CAN_SHRIEKSIGN)
/* v */ SIZE16,
#else
0.#endif
#ifdef PERL_PACK_CAN_SHRIEKSIGN
/* v */ SIZE16,
#else
0.#endif
Copy the code

#ifdef is a shorthand, but does not support more complex expressions.

#ifdef HAVE_MYHEADER
# if VERSION > 3.# endif
#endif
Copy the code

In this case, use

#if defined(HAVE_MYHEADER) && VERSION > 3.#endif
Copy the code

A more reasonable

The #error command causes the preprocessor to issue error messages, whether or not there is an actual error, for easy debugging.

#if! defined (USE_NUCLEO_144)
 #error "Board Pin number not defined!! Add USE_NUCLEO_144 define within stm32h7xx_nucleo_conf.h file"
#endif
Copy the code

Copyright notice: This article is the original article of CSDN blogger “Sky1203850702”, in accordance with CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement. The original link: blog.csdn.net/sky12038507…