Portabilidad de la directiva # warning preprocessor
Sé que la directiva #warning no es estándar C/C++, pero varios compiladores la soportan, incluyendo gcc/g++. Pero para aquellos que no lo soportan, ¿lo ignorarán silenciosamente o resultará en un error de compilación? En otras palabras, ¿puedo usarlo de forma segura en mi proyecto sin romper la compilación para compiladores que no lo soportan?
6 answers
Es probable que si un compilador no soporta #warning, entonces emitirá un error. A diferencia de #pragma, no hay recomendación de que el preprocesador ignore las directivas que no entiende.
Dicho esto, he usado compiladores en varias plataformas diferentes (razonablemente comunes) y todas han soportado #warning.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-05 03:53:13
Cabe señalar que MSVC utiliza la sintaxis:
#pragma message ( "your warning text here" )
La sintaxis habitual # warning genera un error fatal
C1021: invalid preprocessor command 'warning'
Así que no es portable a esos compiladores.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-11 00:30:06
Es probable que obtenga al menos una advertencia de directiva no reconocida de compiladores que no reconocen #warning, incluso si el bloque de código no está incluido en su compilación. Eso podría o no ser tratado como un error - el compilador podría legítimamente tratarlo como un error, pero muchos serían más laxos.
¿Conoce (puede nombrar) un compilador que no sea GCC/G++ que proporciona #advertencia? [Editado: Sun Solaris 10 (Sparc) y los compiladores de Studio 11 C/C++ aceptan #advertencia.]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-05 04:01:44
Tuve este problema una vez con un compilador para un procesador Atmel. Y generó errores de preprocesador debido al token de advertencia # desconocido.
Desafortunadamente, la solución parecía ser convertir todo el árbol de fuentes para usar el equivalente #pragma y aceptar que el comportamiento de compilación iba a diferir si se usaba gcc.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-05 04:20:20
En realidad, la mayoría de los compiladores que conozco ignoran las directivas #pragma desconocidas y emiten un mensaje de advertencia, por lo que en el peor de los casos, aún recibirá una advertencia.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-05 03:48:19
Al cambiar de mingw a visual studio, agregué tales líneas a mi encabezado de configuración global. (inclúyalo en stdafx.h)
#ifdef __GNUC__
//from https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
//Instead of put such pragma in code:
//#pragma GCC diagnostic ignored "-Wformat"
//use:
//PRAGMA_GCC(diagnostic ignored "-Wformat")
#define DO_PRAGMA(x) _Pragma (#x)
#define PRAGMA_GCC(x) DO_PRAGMA(GCC #x)
#define PRAGMA_MESSAGE(x) DO_PRAGMA(message #x)
#define PRAGMA_WARNING(x) DO_PRAGMA(warning #x)
#endif //__GNUC__
#ifdef _MSC_VER
/*
#define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))
// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF
*/
#define PRAGMA_GCC(x)
// https://support2.microsoft.com/kb/155196?wa=wsignin1.0
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __PRAGMA_LOC__ __FILE__ "("__STR1__(__LINE__)") "
#define PRAGMA_WARNING(x) __pragma(message(__PRAGMA_LOC__ ": warning: " #x))
#define PRAGMA_MESSAGE(x) __pragma(message(__PRAGMA_LOC__ ": message : " #x))
#endif
//#pragma message "message quoted"
//#pragma message message unquoted
//#warning warning unquoted
//#warning "warning quoted"
PRAGMA_MESSAGE(PRAGMA_MESSAGE unquoted)
PRAGMA_MESSAGE("PRAGMA_MESSAGE quoted")
#warning "#pragma warning quoted"
PRAGMA_WARNING(PRAGMA_WARNING unquoted)
PRAGMA_WARNING("PRAGMA_WARNING quoted")
Ahora uso PRAGMA_WARNING (esto necesita ser arreglado)
Tristemente no hay #pragma warning
en el ccg, por lo que advierte pragma no especificado.
Dudo que gcc agregue #pragma warning"
en lugar de microsoft agregando #warning
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-20 07:24:57