Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add my own compiler warning

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

How can I show my own compiler warning?

like image 819
Warpin Avatar asked Jan 26 '10 22:01

Warpin


People also ask

What compiler should I use for c++?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

Which option can be used to display compiler warning?

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

What is a warning message generated by a compiler?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.


2 Answers

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)

like image 109
Jacob Avatar answered Sep 24 '22 03:09

Jacob


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 
like image 21
Georg Fritzsche Avatar answered Sep 21 '22 03:09

Georg Fritzsche