编译器扩展deprecated

本文解析了在使用SVN过程中遇到的废弃函数警告问题,详细介绍了如何通过宏定义识别和处理废弃函数,并解释了编译器扩展__attribute__((deprecated))的作用。

当前的工作与SVN有关,今天使用SVN库编写了一个小程序,编译代码时发现编译器告警:“warning: 'svn_client_ls2' is deprecated (declared at”。

svn@linux-rwdx:~/objs/motadou> make
g++ webdav.cpp -o webdav -I/home/svn/apps/svn/include/subversion-1 -I/home/svn/apps/neon/include/neon -I/home/svn/apps/apr-util/include/apr-1 -I/home/svn/apps/apr/include/apr-1 -I/home/svn/apps/sqlite/include -I/home/svn/apps/ssl/include/openssl\
-L/home/svn/apps/svn/lib -lsvn_client-1 -lsvn_wc-1 -lsvn_fs-1 -lsvn_fs_fs-1 -lsvn_fs_util-1 -lsvn_ra-1 -lsvn_ra_neon-1 -lsvn_ra_local-1 -lsvn_repos-1 -lsvn_delta-1 -lsvn_diff-1 -lsvn_subr-1  -L/home/svn/apps/neon/lib -lneon -L/home/svn/apps/apr-util/lib -laprutil-1 -L/home/svn/apps/apr/lib -lapr-1 -pthread /home/svn/apps/sqlite/lib/libsqlite3.a -L/home/svn/apps/ssl/lib -lssl -lcrypto -lz

webdav.cpp: In function 'int svn_list(Handle&, const std::string&)':
webdav.cpp:120: warning: 'svn_client_ls2' is deprecated (declared at /home/svn/apps/svn/include/subversion-1/svn_client.h:4942)
webdav.cpp:120: warning: 'svn_client_ls2' is deprecated (declared at /home/svn/apps/svn/include/subversion-1/svn_client.h:4942)

于是查看“/home/svn/apps/svn/include/subversion-1/svn_client.h”文件中对函数svn_client_ls2的声明,发现该函数之前有这么一个宏定义“SVN_DEPRECATED”。继续跟踪下去,发现SVN_DEPRECATED是这样定义的:

#ifndef SVN_DEPRECATED
# if !defined(SWIGPERL) && !defined(SWIGPYTHON) && !defined(SWIGRUBY)
#  if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__==3 && __GNUC_MINOR__>=1))
#   define SVN_DEPRECATED __attribute__((deprecated))
#  elif defined(_MSC_VER) && _MSC_VER >= 1300
#   define SVN_DEPRECATED __declspec(deprecated)
#  else
#   define SVN_DEPRECATED
#  endif
# else
#  define SVN_DEPRECATED
# endif
#endif


自此仍不明白该告警的真正含义,于是到网上搜索“__attribute__((deprecated))”,在ARM网站上发现有这样一段提示:

可以使用 deprecated 变量属性声明不提倡使用的变量,而不会导致编译器发出任何警告或错误。但是,对 deprecated 变量的任何访问都会生成警告,但仍会进行编译。警告指出了使用和定义变量的位置。这有助于确定不提倡使用特定定义的原因。


细想之下原来该警告含义是:函数svn_client_ls2已是当前库中不提倡使用的函数,已经有新函数可以取代它。于是SVN的开发者便使用“__attribute__((deprecated))”编译器扩展,来提示开发者该函数已失效,请尽快使用新函数编码。
对该编译器扩展申明的变量或者函数,我们要明白:
1.如果该变量或者函数没有被第三方开发者使用,那么即使开发者使用了包含该函数的库去编译代码,也不会出现任何警告或者错误。
2.如果开发者在代码中引用了被“__attribute__((deprecated))”声明的变量或者函数,那么编译器此时才会出现警告,来提示函数已经过期。

转载于:https://www.cnblogs.com/motadou/archive/2013/01/06/2848354.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值