Board index » cppbuilder » Does #pragma have higher priority over #ifdef?

Does #pragma have higher priority over #ifdef?


2007-04-21 05:38:45 AM
cppbuilder6
In BCB 6, I put a #prama message statement inside a header file within
the header guard, ie:
--- header.h---
#ifndef HeaderH
#define HeaderH
#pragma message ("Some Text Here")
#endif
The header file is included in multiple source files. The message
appears in the compiler output each time one of those source files is
compiled. I expected it to be processed only once. It seems that the
#pragma does not respect the header guard. Is this a bug, or
intentional behavior?
The #pragma in question is inside of a series of #if statements so I
can see which values are being #defined at compile-time, ie:
#undef defines...
#if (VER_MAJOR>3)
// #define 3.8+ values...
#pragma message("4.0 or higher")
#elif (VER_MAJOR == 3)
#if (VER_MINOR>= 9)
// #define 3.8 values...
// #define 3.9+ values...
#pragma message("3.9 or higher")
#elif (VER_MINOR == 8)
// #define 3.8 values...
#pragma message("3.8")
#else
#pragma message("3.7 or earlier")
#endif
#else
#pragma message("earlier than 3.0")
#endif
The correct message is being outputted, just multiple times.
Gambit
 
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote
In BCB 6, I put a #prama message statement inside a header file within
the header guard, ie:
Don't have 6, but BCB5 and BDS both work as expected with the
following code (single message)
// pragma.h
#ifndef HeaderH
#define HeaderH
#pragma message ("Some Text Here")
#endif
// pragma.c
#include "pragma.h"
#include "pragma.h"
#include "pragma.h"
#include "pragma.h"
int main(void)
{
int y = 1; // create waring to ensure message visability
return 0;
}
Quote
The header file is included in multiple source files. The message
appears in the compiler output each time one of those source files is
compiled. I expected it to be processed only once. It seems that the
#pragma does not respect the header guard. Is this a bug, or
intentional behavior?
Isn't each source file being compiled on it's own?
One would expect a message per unit.
Or are you #including them in a "master" unit?
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote
The header file is included in multiple source files. The message
[snip]

The correct message is being outputted, just multiple times.
Isn't this expected behavior? Each translation unit results in a message.
.a
 

{smallsort}

Re:Does #pragma have higher priority over #ifdef?

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
// pragma.c

#include "pragma.h"
#include "pragma.h"
#include "pragma.h"
#include "pragma.h"
Did you try including the header file once in multiple source files
instead?
Quote
Isn't each source file being compiled on it's own?
As part of an overall project.
Gambit
 

Re:Does #pragma have higher priority over #ifdef?

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Isn't this expected behavior? Each translation unit results in a
message.
If that is the case, then my understanding of how the preprocessor
works is way off.
Gambit
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote
"Bob Gonder" wrote in message

>// pragma.c
>
>#include "pragma.h"
>#include "pragma.h"
>#include "pragma.h"
>#include "pragma.h"

Did you try including the header file once in multiple source files
instead?
You mean like
//GenericN.c
#include "pragma.h"
//main.c
#include "Generic1.c"
#include "Generic2.c"
Should be the same.
Quote
>Isn't each source file being compiled on it's own?

As part of an overall project.
Each generated *.obj file is a seperate compile and each will generate
the message (if that header is a part of it)
The *compiler* has no sense of *project*.
In this reguard, I don't think we've yet staryed far from Make.
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote
If that is the case, then my understanding of how the preprocessor
works is way off.
Were you perhaps thinking Precompiled Headers would suppress the
message on subsequent use?
 

Re:Does #pragma have higher priority over #ifdef?

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
//main.c
#include "Generic1.c"
#include "Generic2.c"
No, not including .c files like that. I'm not doing a command-line C
compile. I'm doing a full VCL C++ compile. So a .bpr project file
with references to the individual .cpp files.
Gambit
 

Re:Does #pragma have higher priority over #ifdef?

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
Were you perhaps thinking Precompiled Headers would
suppress the message on subsequent use?
I was expecting the header guard to be global throughout the entire
project as soon as it is defined for the first time, so that the
contents of the header file are only compiled once and then reused
when needed.
Gambit
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote
If that is the case, then my understanding of how the preprocessor
works is way off.

Not anymore :)
 

Re:Does #pragma have higher priority over #ifdef?

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote
I was expecting the header guard to be global throughout the entire
project as soon as it is defined for the first time, so that the
contents of the header file are only compiled once and then reused
when needed.
Nope, completely wrong. Each translation unit (read - source file) is
processed independently. Nothing, absolutely nothing, may be carried
over to the next compilation.
The linker was invented for stuff that has to be matched up across
object files.
And preprocessed headers exist so that the compiler can squirrel away a
whole current context and reload it when compiling the next source file.
Alan Bellingham
--
Team Thai Kingdom
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote

I'm doing a full VCL C++ compile. So a .bpr project file
with references to the individual .cpp files.
Yeah, that's what I thought you meant, but I couldn't believe Remy
thought that was the case.
I put it down to Fridayitis.
 

Re:Does #pragma have higher priority over #ifdef?

Remy Lebeau (TeamB) wrote:
Quote

I was expecting the header guard to be global throughout the entire
project as soon as it is defined for the first time, so that the
contents of the header file are only compiled once and then reused
when needed.
At this point that would probably be breaking.
Consider that each unit is allowed to have its own individual compiler
settings and Defines. And even with global settings, the individual
sources might add overrides in code.
I think at this time, PCH are just compiler-friendly versions of the
user-friendly headers, distilled into a single file, and as such are
mostly just "fast-reading".
The header guards ensure that the header is included in the PCH once.
The PCH is read (or remembered) and processed for each unit.
In your scenario, you are expecting pragma message to be suppressed
after the first unit. What else should be suppressed ? If nothing
else, why suppress only pragma message?
Nothing fundamental has changed on the compiler front for several
decades. I don't see how any fundamental change can be made to the
process without at least the posibility of breaking existing code.
"Make" (and the IDE is just a fancy Make) is just a batch wrapper.
We now have concurrent compiling, but that again, is just a parallel
batch with each thread doing its own thing. There is also JIT and
instant compile, but again, those are unit based (single-batch).
The only way you are going to get the behaviour you are espousing is
to have One compile unit that #includes all of the source files. That
was the point of the other sub-thread.
</enddeadhorsebeating>
 

Re:Does #pragma have higher priority over #ifdef?

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote
"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Were you perhaps thinking Precompiled Headers would
>suppress the message on subsequent use?

I was expecting the header guard to be global throughout the entire
project as soon as it is defined for the first time, so that the
contents of the header file are only compiled once and then reused
when needed.
Consider two .cpp files that both #include <map>. Obviously what you
describe above cannot be true, or else one of the files would get the
std::map definition, and the other would fail to compile.
--
Chris (TeamB);