C++ 如何摆脱 GCC 中已弃用函数中已弃用的警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13459602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get rid of deprecated warnings in deprecated functions in GCC?
提问by Magnus Hoff
One way to implement deprecation warnings is to produce warnings on calls to deprecated functions, unless you are calling from a deprecated context. This way legacy code can call legacy code without producing warnings that only amount to noise.
实现弃用警告的一种方法是在调用弃用函数时产生警告,除非您从弃用的上下文中调用。通过这种方式,遗留代码可以调用遗留代码而不会产生只会产生噪音的警告。
This is a reasonable line of thinking, and it is reflected in the implementations I see in GCC 4.2 (1) and Clang 4.0 (2) on OS X as well as Clang 3.0 (3) on Ubuntu.
这是一个合理的思路,它反映在我在 OS X 上的 GCC 4.2 (1) 和 Clang 4.0 (2) 以及 Ubuntu 上的 Clang 3.0 (3) 中看到的实现中。
- (1): i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
- (2): Apple clang version 4.0 (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn)
- (3): Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
- (1): i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1(基于 Apple Inc. build 5658)(LLVM build 2336.11.00)
- (2): Apple clang 4.0 (tags/Apple/clang-421.0.57) (基于LLVM 3.1svn)
- (3): Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final)(基于LLVM 3.0)
However, when I compile with GCC 4.6 (4) on Ubuntu, I get deprecated warnings for all invocations of deprecated functions, independently of context. Is this a regression in functionality? Are there compiler options I can use to get the other behavior?
但是,当我在 Ubuntu 上使用 GCC 4.6 (4) 进行编译时,对于所有不推荐使用的函数的调用,我都会收到不推荐使用的警告,与上下文无关。这是功能上的回归吗?我可以使用编译器选项来获得其他行为吗?
- (4): g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
- (4): g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Example program:
示例程序:
int __attribute__((deprecated)) a() {
return 10;
}
int __attribute__((deprecated)) b() {
return a() * 2; //< I want to get rid of warnings from this line
}
int main() {
return b(); //< I expect a warning on this line only
}
Output from GCC 4.2 (Yes, I do get the same warning twice. I don't care about that, though):
GCC 4.2 的输出(是的,我确实收到了两次相同的警告。不过我不在乎):
main.cpp: In function ‘int main()':
main.cpp:10: warning: ‘b' is deprecated (declared at main.cpp:5)
main.cpp:10: warning: ‘b' is deprecated (declared at main.cpp:5)
Output from GCC 4.6:
GCC 4.6 的输出:
main.cpp: In function 'int b()':
main.cpp:6:9: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations]
main.cpp:6:11: warning: 'int a()' is deprecated (declared at main.cpp:1) [-Wdeprecated-declarations]
main.cpp: In function 'int main()':
main.cpp:10:9: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations]
main.cpp:10:11: warning: 'int b()' is deprecated (declared at main.cpp:5) [-Wdeprecated-declarations]
How can I convince GCC 4.6 that it should give me the same output as GCC 4.2?
我如何说服 GCC 4.6 它应该给我与 GCC 4.2 相同的输出?
采纳答案by Magnus Hoff
The behaviour you're seeing in GCC 4.2 is caused by an Apple-specific patch to GCC. FSF GCC 4.2.4 warns about the use of a
. The specific bit that Apple GCC has that FSF GCC doesn't is:
您在 GCC 4.2 中看到的行为是由 Apple 特定的 GCC 补丁引起的。FSF GCC 4.2.4 警告使用a
. Apple GCC 具有而 FSF GCC 没有的特定位是:
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -902,6 +902,9 @@ warn_deprecated_use (tree node)
if (node == 0 || !warn_deprecated_decl)
return;
+ if (current_function_decl && TREE_DEPRECATED (current_function_decl))
+ return;
+
if (DECL_P (node))
{
expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
(Available under GPLv2 or later)
(在 GPLv2 或更高版本下可用)
You may wish to adapt this patch to a later version of GCC (perhaps no changes are needed, perhaps major changes are needed), and build GCC from source with this patch applied. Or you may report this as a feature request on FSF GCC bugzilla.
您可能希望将此补丁调整到更高版本的 GCC(也许不需要更改,也许需要进行重大更改),并在应用此补丁的情况下从源代码构建 GCC。或者您可以将此报告为 FSF GCC bugzilla 上的功能请求。
回答by doron
-Wno-deprecated
will remove all deprecated warnings
-Wno-deprecated
将删除所有已弃用的警告
回答by David Hammen
gcc 4.6 added diagnostic pragmas that will help solve this problem:
gcc 4.6 添加了有助于解决此问题的诊断编译指示:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
int __attribute__((deprecated)) b() {
return a() * 2; //< I want to get rid of warnings from this line
}
#pragma GCC diagnostic pop
Note: This only works in gcc 4.6 and higher. The push
and pop
are 4.6 extensions. With gcc 4.5, the #pragma GCC diagnostic push
and pop
will be ignored (with warnings). What won't be ignored is the #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-- but now this has effect until end of file.
注意:这只适用于 gcc 4.6 及更高版本。的push
和pop
是4.6扩展。使用 gcc 4.5,#pragma GCC diagnostic push
和pop
将被忽略(带有警告)。不会被忽略的是#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-- 但现在这在文件结束之前一直有效。
回答by wincrasher
I encounter the same issue. The solution that came up is the following
我遇到了同样的问题。提出的解决方案如下
typedef OLD_A_NOT_TO_BE_USED a __attribute__((deprecated));
int OLD_A_NOT_TO_BE_USED () {
return 10;
}
int __attribute__((deprecated)) b() {
return OLD_A_NOT_TO_BE_USED () * 2; //< I want to get rid of warnings from this line
}
int main() {
return b(); //< I expect a warning on this line only
}
So I just rename my a class into OLD_A_NOT_TO_BE_USED class. I get the warning only on return b(); and if someone was using a they will still get the deprecated warning.
所以我只是将我的类重命名为 OLD_A_NOT_TO_BE_USED 类。我只在 return b(); 时收到警告;如果有人使用 a 他们仍然会收到已弃用的警告。