1、此函数属性指示存在某个函数,
如果使用了 deprecated 属性修饰的函数,则编译器必须生成警告。
如果未使用该属性修饰的函数则不会产生任何警告
2、在GNU模式下,这个属性接受一个可选的字符串参数来显示在消息中,即:
__attribute__((deprecated("message")))
3、例程
#include <stdio.h>
__attribute__((deprecated("Function_Attributes_deprecated_0 is renamed to func_dec()"))) int Function_Attributes_deprecated_0(int b)
{
return b++;
}
int main(int argc, char *argv[])
{
Function_Attributes_deprecated_0(2);
int choice = 2;
switch (choice)
{
case 1:
printf("This is case 1.\n");
break;
case 2:
printf("This is case 2.\n");
__attribute__((fallthrough));
case 3:
printf("This is case 3.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
4、编译结果
test.c: In function ‘main’:
test.c:10:5: warning: ‘Function_Attributes_deprecated_0’ is deprecated: Function_Attributes_deprecated_0 is renamed to func_dec() [-Wdeprecated-declarations]
10 | Function_Attributes_deprecated_0(2);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:3:95: note: declared here
3 | __attribute__((deprecated("Function_Attributes_deprecated_0 is renamed to func_dec()"))) int Function_Attributes_deprecated_0(int b)
产生了警告,提示该函数已经废弃
5、使用场景
代码版本升级时,将 API 重命名了,那么当程序中还在使用旧的 API 名称时就会产生警告,提示用户使用新的 API