pragmas vs preprocessors

Dan Nydick nydick at psc.edu
Fri Jan 11 02:31:17 AEST 1991


I have an application where I'd like to have a macro expand into
a #pragma.  I tried the following:

----
#define THISISAPRAGMA #pragma something

main()
{
   /* blah blah */
THISISAPRAGMA
   /* blah blah */
}
----

I could imagine several interpretations for this...
	1) insert and recognize the pragma in "main".
	2) complain about the # in the macro definition.
	3) insert "#pragma something" in main, but don't
		recognize it as a pragma.
	4) don't expand THISISAPRAGMA at all.
A quick scan of the C standard says that the # is illegal in
a "functionlike" definition, but didn't rule it out in an
"objectlike" definition.

Anyway, various compilers I've tried have resulted in 2, 3,
and 4.  I haven't found a compiler which does option 1.
I am able to run the code through the preprocessor and then
feed the output back into the compiler to get the effect of
option 1 with one of my compilers.

Should this work?  Which option is correct?  How would one
define a macro which expands into a pragma?  Or even, how
could one write a conditional expression so the pragma
only appears if certain symbols are defined (plain ifdefs
around the pragma are not good enough since some non-ansi
compilers will complain about #pragma even inside a "false"
condition)?  Is a #pragma supposed to be significant to the
compiler or to the pre-processor?

My current workaround is to conditionally include a file
which contains the #pragma line.

Thanks in advance.

Dan Nydick, Pittsburgh Supercomputing Center
nydick at psc.edu



More information about the Comp.std.c mailing list