C++ 添加我自己的编译器警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143352/
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
Add my own compiler warning
提问by Warpin
When using sprintf, the compiler warns me that the function is deprecated.
使用 sprintf 时,编译器会警告我该函数已被弃用。
How can I show my own compiler warning?
如何显示我自己的编译器警告?
采纳答案by Jacob
In Visual Studio,
#pragma message ("Warning goes here")
#pragma message ("Warning goes here")
On a side note, if you want to suppress such warnings, find the compiler warning ID (for the deprecated warning, it's C4996
) and insert this line:
附带说明一下,如果您想取消此类警告,请找到编译器警告 ID(对于已弃用的警告,它是C4996
)并插入以下行:
#pragma warning( disable : 4996
)
#pragma warning( disable : 4996
)
回答by Georg Fritzsche
Although there is no standard #warning
directice, many compilers (including GCC, VC, Intels and Apples), support #warning message
.
尽管没有标准#warning
指令,但许多编译器(包括 GCC、VC、Intel 和 Apple)都支持#warning message
.
#warning "this is deprecated"
Often it is better to not only bring up a warning (which people can overlook), but to let compiling fail completely, using the #error
directive (which is standard):
通常最好不仅提出警告(人们可以忽略),而且让编译完全失败,使用#error
指令(这是标准的):
#if !defined(FOO) && !defined(BAR)
# error "you have neither foo nor bar set up"
#endif
回答by Martin B
To mark a function as deprecated, use __declspec(deprecated)
, e.g.
要将函数标记为已弃用,请使用__declspec(deprecated)
,例如
__declspec(deprecated) void f();
回答by noelicus
In VC if you want the warning to show up in the warning count at the end of compilation you need to use this format:
在 VC 中,如果您希望在编译结束时在警告计数中显示警告,则需要使用以下格式:
#pragma message(": warning<put what you like here>: blah blah blah")
The important sequence is: colon, space, "warning", something or nothing, colon, "your warning text"
重要的顺序是:冒号、空格、“警告”、某事或无、冒号、“你的警告文本”
If you want to be fancy then file and line number can be added before the 1st colon so you can double click it to jump to the code (from microsoft.com):
如果你想花哨,那么可以在第一个冒号之前添加文件和行号,这样你就可以双击它跳转到代码(来自microsoft.com):
// pragma_directives_message1.cpp // compile with: /LD #if _M_IX86 >= 500 #pragma message("_M_IX86 >= 500") #endif #pragma message("") #pragma message( "Compiling " __FILE__ ) #pragma message( "Last modified on " __TIMESTAMP__ ) #pragma message("") // with line number #define STRING2(x) #x #define STRING(x) STRING2(x) #pragma message (__FILE__ "[" STRING(__LINE__) "]: test") #pragma message("")
// pragma_directives_message1.cpp // compile with: /LD #if _M_IX86 >= 500 #pragma message("_M_IX86 >= 500") #endif #pragma message("") #pragma message( "Compiling " __FILE__ ) #pragma message( "Last modified on " __TIMESTAMP__ ) #pragma message("") // with line number #define STRING2(x) #x #define STRING(x) STRING2(x) #pragma message (__FILE__ "[" STRING(__LINE__) "]: test") #pragma message("")
回答by Blackphoenix
I think this should work
我认为这应该有效
void foo () __attribute__ ((deprecated("This function is deprecated. \nFor further information please refer to the README")));