ffmpeg 中的 attribute_deprecated 屬性

    閱讀ffmpeg源碼是 發現一些函數前面加了 attribute_deprecated 屬性;如:attribute_deprecated int url_fopen( AVIOContext **s, const char *url, int flags);
在libavutil/attributes.h  中有如下定義:
   83 #ifndef attribute_deprecated
   84 #if AV_GCC_VERSION_AT_LEAST(3,1)
   85 #    define attribute_deprecated __attribute__((deprecated))
   86 #else
   87 #    define attribute_deprecated
   88 #endif
   89 #endif

__attribute__ 語法爲GNU C 的特性,__attribute__可以設置函數屬性(Function Attribute)、變量屬性(Variable Attribute)和類型屬性(Type Attribute)。
__attribute__語法格式爲:__attribute__ ((attribute))
需要注意的是: 使用__attribute__的時候,只能函數的聲明處使用__attribute__,並且在“;“前。

在開發一些庫的時候,API的接口可能會過時,爲了提醒開發者這個函數已經過時。只要函數被使用,在編譯是都會產生警告,警告信息中包含過時接口的名稱及代碼中的引用位置。
下面是GNU 網站(http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html)上對這個屬性的解釋:
deprecated
The deprecated attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. Note that the warnings only occurs for uses:
          int old_fn () __attribute__ ((deprecated));
          int old_fn ();
          int (*fn_ptr)() = old_fn;
     
results in a warning on line 3 but not line 2.
下面是一個列子:
root@wang:/work/wanghuan/gnu# cat gnu.c 
#include <stdlib.h>
#include <stdio.h>

__attribute__((deprecated)) void attribute();
void attribute()
{
printf("GNU attribute \n");
}

int main()
{
attribute();
return 0;
}
root@wang:/work/wanghuan/gnu# gcc gnu.c -o gnu 
gnu.c: In function ‘main’:
gnu.c:12: warning: ‘attribute’ is deprecated (declared at gnu.c:5)     //編譯警告
root@wang:/work/wanghuan/gnu# ./gnu 
GNU attribute 

關於__attribute__屬性,有多種類型,由於ARM編譯器支持GNU語法,在ARM的網站http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348bc/Caccahah.html 有這些特性的詳細介紹。

轉載自
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章