When using sprintf, the compiler warns me that the function is deprecated.
How can I show my own compiler warning?
When using sprintf, the compiler warns me that the function is deprecated.
How can I show my own compiler warning?
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
)
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
To mark a function as deprecated, use __declspec(deprecated)
, e.g.
__declspec(deprecated) void f();
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("")
I think this should work
void foo () __attribute__ ((deprecated("This function is deprecated. \nFor further information please refer to the README")));
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.