35

When using sprintf, the compiler warns me that the function is deprecated.

How can I show my own compiler warning?

Warpin
  • 6,971
  • 12
  • 51
  • 77

6 Answers6

32

In Visual Studio,

#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:

#pragma warning( disable : 4996)

Jacob
  • 34,255
  • 14
  • 110
  • 165
  • This doesn't do exactly what Martin wants, though -- he wants the warning to be issued when the function is _used_, not when it is compiled. – Martin B Jan 26 '10 at 23:06
  • 1
    I guess my question could have be read either way (sorry for that!), but this one was what I was looking for. – Warpin Jan 26 '10 at 23:16
28

Although there is no standard #warning directice, many compilers (including GCC, VC, Intels and Apples), support #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):

#if !defined(FOO) && !defined(BAR)
#  error "you have neither foo nor bar set up"
#endif
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
18

To mark a function as deprecated, use __declspec(deprecated), e.g.

__declspec(deprecated) void f();
Martin B
  • 23,670
  • 6
  • 53
  • 72
14

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:

#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):

// 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("")
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • This works also with gcc where "#error" works but "#warning" unfortunately not. But it will always output a "note:" reference so output filters might not catch the message as error. Still we don't have a perfect solution. – Lothar Apr 12 '21 at 10:33
1

I think this should work

void foo () __attribute__ ((deprecated("This function is deprecated. \nFor further information please refer to the README")));
Blackphoenix
  • 133
  • 2
  • 2
  • 9
1

The answer from noelicus is great, but I had to use round brackets for the line number instead of square brackets.

This means their code

// with line number  
#define STRING2(x) #x  
#define STRING(x) STRING2(x)  

#pragma message (__FILE__ "[" STRING(__LINE__) "]: test")

should be

// with line number  
#define STRING2(x) #x  
#define STRING(x) STRING2(x)  

#pragma message (__FILE__ "(" STRING(__LINE__) "): test")

With a message which is actually interpreted by the compiler as a warning:

#define STRING2(x) #x  
#define STRING(x) STRING2(x)  
#pragma message(__FILE__ "(" STRING(__LINE__) "): warning: My own compiler warning, which can be double-clicked to go to the source code line.")

I'm using Visual Studio 2022 with compiling VS2017 projects, if this makes a difference.

Dirksche
  • 21
  • 3