1、attribute
关键字__attribute__也可以对结构体(struct)或共用体(union)进行属性设置。大致有六个参数值可以被设定,即:aligned, packed, transparent_union, unused, deprecated和may_alias。
- attribute((deprecated)) 管理过时的代码
在开发一些库的时候,API的接口可能会过时,为了提醒开发者这个函数已经过时。可以在函数声明时加上attribute((deprecated))属性。这样,只要函数被使用,在 编译是都会产生警告 ,警告信息中包含过时接口的名称及代码中的引用位置。
示例如下:
旧版本代码:
typedef struct {
float left;
float top;
float right;
float bottom;
float score;
float class_label;
} bbox_type_t;
3.7.1新版本,将结构体命名更改:
#define HBRT_DEPRECATED_NAME(new_name, old_name, version_since) \
__attribute__ ((deprecated(#old_name " has been superseded by " #new_name " since " #version_since ", and will be removed in a future release")))
typedef struct {
float left;
float top;
float right;
float bottom;
float score;
float class_label;
} bbox_float_type_t;
typedef bbox_float_type_t bbox_type_t HBRT_DEPRECATED_NAME(bbox_float_type_t , bbox_type_t, 3.7.1);
用typedef 兼容前后版本的使用,再用__attribute__ ((deprecated))的方式声明该命名已经过时,在编译时产生警告。