1 Introduction
Compilation warnings and compilation errors are very common things for programmers. But for serious programmers, any warning is intolerable.
2. Use wisely
#error
It is often used when setting up an environment or transplanting an operating system. It is used to tell the transplanter that specified configuration is required, otherwise it will not be used. For example, the following code:
#if !defined (STM32F2XX)
#error "Please select first the target STM32F2XX device used in your application (in stm32f2xx.h file)"
#endif
The above code shows that if STM32F2XX
this macro is not defined, it will not be compiled. In other words, if the program is to be compiled successfully, STM32F2XX must be defined.
warning
The severity level of #error is not as high as that of #error. It is often used to remind programmers that this place may need to be modified according to the actual situation of the program. If it is not modified, it will be executed by default. For example, the following code:
#ifdef VECTOR_BASE
SCB->VTOR = VECTOR_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation */
#else
#warning no define VECTOR_BASE
#endif
The above code shows that if it is defined, VECTOR_BASE
the interrupt vector will be configured according to the defined value. If VECTOR_BASE is not defined, the interrupt vector configuration will be skipped and the reset value will be used. However, a warning will be issued during compilation to remind the programmer whether it is necessary to configure the interrupt vector according to the defined value. Actual configuration is required.
3. Actively block specific warnings
In some scenarios, we can also actively block specific warnings within specific code or file scopes.
For example, if the code writes a function "void TestFunction(void)", but it has not been called yet, you will get a Wunused-function
warning if you compile it directly. But if you add the following statement to your code:
#pragma GCC diagnostic ignored "-Wunused-function"
/* 此区间内,如果出现函数仅申明,但是没有被调用时,编译不产生warning */
void TestFunction(void)
{
flash_test();
}
#pragma GCC diagnostic pop
Because we configured "-Wunused-function", even if TestFunction is not called during compilation, compilation will not generate a warning.
In addition, we can also consult the GCC compiler documentation to discover more warning options and actively block more warnings as needed.