添加我自己的编译器警告

时间:2010-01-26 22:59:22

标签: c++ windows visual-c++

使用sprintf时,编译器警告我该函数已弃用。

如何显示自己的编译器警告?

6 个答案:

答案 0 :(得分:25)

Visual Studio

#pragma message ("Warning goes here")

在旁注中,如果you want to suppress such warnings,找到编译器警告ID(对于已弃用的警告,它是C4996)并插入此行:

#pragma warning( disable : 4996

答案 1 :(得分:24)

虽然没有标准的#warning指南,但许多编译器(包括GCC,VC,Intels和Apples)都支持#warning message

#warning "this is deprecated"

通常最好不仅提出警告(人们可以忽略),而是使用#error指令(标准)让编译完全失败:

#if !defined(FOO) && !defined(BAR)
#  error "you have neither foo nor bar set up"
#endif

答案 2 :(得分:14)

要将某个功能标记为已弃用,请使用__declspec(deprecated),例如

__declspec(deprecated) void f();

答案 3 :(得分:10)

在VC中,如果您希望警告在编译结束时显示在警告计数中,则需要使用以下格式:

#pragma message(": warning<put what you like here>: blah blah blah")

重要的序列是:冒号,空格,“警告”,什么都没有,冒号,“你的警告文字”

如果您想要花哨,那么可以在第一个冒号之前添加文件和行号,这样您就可以双击它以跳转到代码(来自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("")

答案 4 :(得分:0)

答案 5 :(得分:0)

我认为这应该有效     

void foo () __attribute__ ((deprecated("This function is deprecated. \nFor further information please refer to the README")));