Preprocessor Directives

D

Daniel Rudy

Hello Group,

Is there a preprocessor directive that will cause the compiler to print
a warning diagnostic of whatever message that I choose? I'm thinking of
something along the lines of:

#ifndef uint64
#warning 64-bit mode not supported.
#endif

-or-

#ifndef uint64
#error 64-bit support is required.
#endif


Well?
 
R

Richard Heathfield

Daniel Rudy said:
Hello Group,

Is there a preprocessor directive that will cause the compiler to
print
a warning diagnostic of whatever message that I choose?

Your best bet, and it is not a good bet, is a #pragma, if your compiler
happens to support one that does what you want. (Some do, certainly.)
This, of course, renders your code non-portable.

I'm thinking
of something along the lines of:

#ifndef uint64
#warning 64-bit mode not supported.
#endif

-or-

#ifndef uint64
#error 64-bit support is required.
#endif

The latter is well-defined by ISO C, but has the (unfortunate, in your
case) side-effect of terminating the compilation. If that's acceptable
to you, then that's your answer. Otherwise, you may be forced to go for
a #pragma.
 
S

santosh

"]

Hello Group,

Is there a preprocessor directive that will cause the compiler to print
a warning diagnostic of whatever message that I choose? I'm thinking of
something along the lines of:

#ifndef uint64
#warning 64-bit mode not supported.
#endif

-or-

#ifndef uint64
#error 64-bit support is required.
#endif


Well?[/QUOTE]

Using the #error directive will cause the translation to abort. Most
compilers provide extensions for printing a message and continuing
compilation.
 
J

jacob navia

Daniel said:
Hello Group,

Is there a preprocessor directive that will cause the compiler to print
a warning diagnostic of whatever message that I choose? I'm thinking of
something along the lines of:

#ifndef uint64
#warning 64-bit mode not supported.
#endif

-or-

#ifndef uint64
#error 64-bit support is required.
#endif


Well?
The lcc-win32 compiler supports
#warning

That does exactly what you want.
The gcc compiler supports that feature too.
Microsoft doesn't support it.
 
R

Richard Heathfield

jacob navia said:

The lcc-win32 compiler supports
#warning

That does exactly what you want.
The gcc compiler supports that feature too.
Microsoft doesn't support it.

You can fake it, though, with #pragma message() - if you mimic the
Microsoft warning format, you can even trick the IDE into letting you
double-click to jump to the relevant code, just as if it were a "real"
warning.

About ten years ago, I hacked together a Y2K code-searching tool in this
way, using #pragma message("warning...") as a fast-and-filthy hypertext
substitute.
 
R

Roberto Waltman

Daniel said:
Is there a preprocessor directive that will cause the compiler to print
a warning diagnostic of whatever message that I choose?
...


As others pointed out, only "#error" is available, and it will stop
the compilation.
If you can tolerate a misleading warning, the following macro is a
"shoot from the hip", non-portable, very poor attempt to show a
warning by creating something else the compiler should warn you about.

Caveats:
(a) Can be used only inside functions.
(b) The warning text must be a valid C identifier
(almost, it may begin with a digit):
WARNING(about_something) is OK,
WARNING(something happened!!) is not.
(c) Since it creates a block, it may alter flow control:
if (condition)
WARNING(xxx)
process(condition)
is not what it looks.
(d) Probably other things I did not think of.

Outside functions, a similar macro could create a prototype for a
static function with the text embedded in the function name.
With the right parameters gcc will warn of a function declared static
but never defined.

--------------------
$ cat wtest.c

/* use inside functions only, be aware of side effects */
#define WARNING(x) { static char WARNING_ ## x; }

int main(void)
{
#if ODD
WARNING(odd)
#else
WARNING(even)
#endif

return 0;
}

$
$ gcc -ansi -Wall -DODD=1 wtest.c
wtest.c: In function 'main'
wtest.c:8: warning: unused variable 'WARNING_odd'
$
$ gcc -ansi -Wall -DODD=0 wtest.c
wtest.c: In function 'main'
wtest.c:10: warning: unused variable 'WARNING_even'
$

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
D

Daniel Rudy

At about the time of 7/18/2007 2:44 AM, jacob navia stated the following:
The lcc-win32 compiler supports
#warning

That does exactly what you want.
The gcc compiler supports that feature too.
Microsoft doesn't support it.

At the risk of going off-topic, how do you do that in gcc? I've been
through the manual and I don't see anything relating to printing a
warning diagnostic (of my choice, of course) from the preprocessor. I
did find the #error directive after I posted the initial message though.

This may be a useful link to have somewhere in the FAQ:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
 
D

Daniel Rudy

At about the time of 7/18/2007 11:29 AM, Roberto Waltman stated the
following:
As others pointed out, only "#error" is available, and it will stop
the compilation.

I found the #error directive after I posted the initial message.
If you can tolerate a misleading warning, the following macro is a
"shoot from the hip", non-portable, very poor attempt to show a
warning by creating something else the compiler should warn you about.

Caveats:
(a) Can be used only inside functions.
(b) The warning text must be a valid C identifier
(almost, it may begin with a digit):
WARNING(about_something) is OK,
WARNING(something happened!!) is not.
(c) Since it creates a block, it may alter flow control:
if (condition)
WARNING(xxx)
process(condition)
is not what it looks.
(d) Probably other things I did not think of.

Outside functions, a similar macro could create a prototype for a
static function with the text embedded in the function name.
With the right parameters gcc will warn of a function declared static
but never defined.

This cannot be inside a function...and I need it to print a message of
my choice, like error does.
--------------------
$ cat wtest.c

/* use inside functions only, be aware of side effects */
#define WARNING(x) { static char WARNING_ ## x; }

int main(void)
{
#if ODD
WARNING(odd)
#else
WARNING(even)
#endif

return 0;
}

$
$ gcc -ansi -Wall -DODD=1 wtest.c
wtest.c: In function 'main'
wtest.c:8: warning: unused variable 'WARNING_odd'
$
$ gcc -ansi -Wall -DODD=0 wtest.c
wtest.c: In function 'main'
wtest.c:10: warning: unused variable 'WARNING_even'
$

That's kinda a round about way of doing it I suppose. I'm not that
picky, but I'm looking for something more like this:

#ifndef _LONGLONG
#warning 64-bit support should be enabled for correct functioning of
program.
#endif
 
S

santosh

At about the time of 7/18/2007 2:44 AM, jacob navia stated the
following:
[ ... ]
At the risk of going off-topic, how do you do that in gcc? I've
been through the manual and I don't see anything relating to
printing a warning diagnostic (of my choice, of course) from the
preprocessor.

You need to look in the manual for the C preprocessor. That's info
cpp for Linux. For other system's it might be man cpp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top