单片机型号:STM32F091RCT6
尝试在PlatformIO中使用STM32Cube进行项目的开发工作,第1次编译,即出现如下图的警告。
该警告在GCC中有详尽的解释:
Allows the compiler to assume the strictest aliasing rules applicable to the language being compiled. For C (and C++), this activates optimizations based on the type of expressions. In particular, an object of one type is assumed never to reside at the same address as an object of a different type, unless the types are almost the same. For example, an "unsigned int" can alias an "int", but not a "void*" or a "double". A character type may alias any other type.
简而言之, 在该参数激活的情况下,编译器希望不同类型的对象不会指向同一个地址。比如像这段代码:
int retLen;
someSetFunc((unsigned long*)&retLen);
printf("ret len = %d\n",retLen);
实际上,我们在实际开发过程中,是不会出现问题的,如果是自己写的代码,可以严格要求自己,进行修正,修正代码如下:
union u_retLen
{
int retLen;
unsigned long ptr;
};
someSetFunc(&u_retLen.ptr);
printf("ret len = %d\n",u_retLen.retLen);
但如果不是自己的代码,进行修正终归不好,但可以采取下列方法予以屏蔽。
1、打开出现警告的文件,本例中是
C:\.platformio\packages\framework-stm32cube\f0\Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_crc.c
2、在程序最前方,加入下列语句即可。-Wstrict-aliasing即为希望忽略的警告。
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
3、再次编译,没有任何警告出现。