用__attribute__((deprecated))管理过时的代码
2016-08-30 13:53
274 查看
在开发一些库的时候,API的接口可能会过时,为了提醒开发者这个函数已经过时。可以在函数声明时加上attribute((deprecated))属性,如此,只要函数被使用,在编译是都会产生警告,警告信息中包含过时接口的名称及代码中的引用位置。
attribute可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
attribute语法格式为:attribute ((attribute))
注意: 使用attribute的时候,只能函数的声明处使用attribute,
在编译时会出现类似警告:
# gcc attribute_deprecated.c -o test
attribute_deprecated.c: In function ‘main’:
attribute_deprecated.c:33: warning: ‘variable_old’ is deprecated (declared at attribute_deprecated.c:20)
attribute_deprecated.c:35: warning: ‘function_old’ is deprecated (declared at attribute_deprecated.c:25)
attribute可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
attribute语法格式为:attribute ((attribute))
注意: 使用attribute的时候,只能函数的声明处使用attribute,
#include <stdio.h> #include <stdlib.h> #ifdef __GNUC__ # define GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y)) #else # define GCC_VERSION_AT_LEAST(x,y) 0 #endif #if GCC_VERSION_AT_LEAST(3,1) # define attribute_deprecated __attribute__((deprecated)) #elif defined(_MSC_VER) # define attribute_deprecated __declspec(deprecated) #else # define attribute_deprecated #endif /* Variable Attribute */ attribute_deprecated int variable_old = 0; /* Function Attribute */ attribute_deprecated void function_old(void); void function_old(void) { printf("old function.\n"); return; } int main(void) { variable_old++; function_old(); return EXIT_SUCCESS; }
在编译时会出现类似警告:
# gcc attribute_deprecated.c -o test
attribute_deprecated.c: In function ‘main’:
attribute_deprecated.c:33: warning: ‘variable_old’ is deprecated (declared at attribute_deprecated.c:20)
attribute_deprecated.c:35: warning: ‘function_old’ is deprecated (declared at attribute_deprecated.c:25)
相关文章推荐
- C++库研究笔记--用__attribute__((deprecated)) 管理过时代码
- C++库研究笔记--用__attribute__((deprecated)) 管理过时代码
- C++库研究笔记--用__attribute__((deprecated)) 管理过时代码
- 微软项目管理[EPM]数据库剖析4:项目大纲代码的四张表
- 在项目管理中引入代码质量审核员
- 微软项目管理[EPM]数据库剖析2:如何取得全局项目中某个大纲代码的列表值
- Microsoft .NET Framework SDK Net Classes:用管理代码编写联网应用程序
- 代码管理(2)
- [个体软件过程]之缺陷管理--C++代码复查指南和检查表
- 如何完全清除VSS的代码管理
- 最近设计了一个生成asp代码的程序,同时也可以作为数据库管理查询的软件,有兴趣的朋友可以去下载!
- [个体软件过程]之缺陷管理--代码复查
- 利用visual source safe管理代码
- 简单SNMP管理程序的VC++代码实例实现
- 代码不好写,管理更不好做。-
- 微软项目管理[EPM]数据库剖析3:如何取得某个项目的某个大纲代码的值
- QuicklyBuildModel/UserManage.aspx[快速类建模型后台用户管理页面代码]
- 代码管理(1)
- 微软项目管理[EPM]数据库应用举例2: 取得一个项目的某大纲代码的值
- 最近项目中一些关于代码编写管理的一些思考