S32DS 消除warning: unused variable
文章目录
前言
在用S32DS开发编译程序时,很多警告把要提示的重要信息淹没了,为了提高开发效率可以过滤掉没用的警告信息。
一、原因分析warning: unused variable
产生该警告的原因是程序中定义的变量没有使用。
二、解决方法
1.用UNUSED宏
https://stackoverflow.com/questions/3599160/how-to-suppress-unused-parameter-warnings-in-c
#define UNUSED(x) (void)(x)
You can use this macro for all your unused parameters. (Note that this works on any compiler.)
For example:
void f(int x) {
UNUSED(x);
...
}
2.添加unused属性告诉编译器
代码如下(示例):
You can use gcc/clang's unused attribute, however I use these macros in a header to avoid having gcc specific attributes all over the source, also having __attribute__ everywhere is a bit verbose/ugly.
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
#ifdef __GNUC__
# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
# define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif
Then you can do...
void foo(int UNUSED(bar)) { ... }
I prefer this because you get an error if you try use bar in the code anywhere so you can't leave the attribute in by mistake.
and for functions...
3.禁用该消息提示
考虑到该警告对程序基本没有不良不影响,因此利用编译指令直接去除该警告。
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
总结
采用第三种方法简单粗暴有效,节省的时间去享受美好的生活不香吗?效果如下,世界顿时安静了。