#ifndef/#ifdef problems in my header file (conditional header inclusion)
Author |
Message |
Mauricio Tavar #1 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
I am finding some very interesting behavior from #define and #ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This file is included by all my routines (they usually are in separate files). Now that would not be very nice since it would include the very same include files (stdio.h and others) over and over again. So, I thought if I use something like #ifndef MOOSE #define MOOSE #include <stdio.h> #include <X11/Xlib.h> #endif the code would only include *one* copy of these two include files. What I am finding out is that is not the case. Running C programs that have similar conditional including of header files in gcc (Sparc), Think C (Mac), Borland TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It really seems that the compiler simply *forgets* what was defined in the previous .c file and define them all over again. As a result, my object files become unnecessarily large. Yes, the executable seems to take care of that, but still I am using more disk space than I should. I am open for suggestions as usual. -- ===========================+======================================= | Mauricio Tavares | "We will attack... |
| | Rimmer | ===========================+=======================================
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
Chao-Kuang Yan #2 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Hi! Have you ever try to put your header in MacHeader file or in pre-compiled headers? That might just do what you intend to do. Mark Yang -- ---------------------------------------------------------------------------- ---- MetaTools Program Team
Quote: > I am finding some very interesting behavior from #define and > #ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This > file is included by all my routines (they usually are in separate files). Now > that would not be very nice since it would include the very same include files > (stdio.h and others) over and over again. So, I thought if I use something > like > #ifndef MOOSE > #define MOOSE > #include <stdio.h> > #include <X11/Xlib.h> > #endif > the code would only include *one* copy of these two include files. What I am > finding out is that is not the case. Running C programs that have similar > conditional including of header files in gcc (Sparc), Think C (Mac), Borland > TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It > really seems that the compiler simply *forgets* what was defined in the > previous .c file and define them all over again. As a result, my object files > become unnecessarily large. Yes, the executable seems to take care of that, > but still I am using more disk space than I should. I am open for suggestions > as usual. > -- > ===========================+======================================= > | Mauricio Tavares | "We will attack... |
> | | Rimmer | > ===========================+=======================================
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
Mauricio Tavare #3 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
I am finding some very interesting behavior from #define and #ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This file is included by all my routines (they usually are in separate files). Now that would not be very nice since it would include the very same include files (stdio.h and others) over and over again. So, I thought if I use something like #ifndef MOOSE #define MOOSE #include <stdio.h> #include <X11/Xlib.h> #endif the code would only include *one* copy of these two include files. What I am finding out is that is not the case. Running C programs that have similar conditional including of header files in gcc (Sparc), Think C (Mac), Borland TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It really seems that the compiler simply *forgets* what was defined in the previous .c file and define them all over again. As a result, my object files become unnecessarily large. Yes, the executable seems to take care of that, but still I am using more disk space than I should. I am open for suggestions as usual. -- ===========================+======================================= | Mauricio Tavares | "We will attack... |
| | Rimmer | ===========================+=======================================
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
Chao-Kuang Yan #4 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Hi! Have you ever try to put your header in MacHeader file or in pre-compiled headers? That might just do what you intend to do. Mark Yang -- ---------------------------------------------------------------------------- ---- MetaTools Program Team
Quote: > I am finding some very interesting behavior from #define and > #ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This > file is included by all my routines (they usually are in separate files). Now > that would not be very nice since it would include the very same include files > (stdio.h and others) over and over again. So, I thought if I use something > like > #ifndef MOOSE > #define MOOSE > #include <stdio.h> > #include <X11/Xlib.h> > #endif > the code would only include *one* copy of these two include files. What I am > finding out is that is not the case. Running C programs that have similar > conditional including of header files in gcc (Sparc), Think C (Mac), Borland > TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It > really seems that the compiler simply *forgets* what was defined in the > previous .c file and define them all over again. As a result, my object files > become unnecessarily large. Yes, the executable seems to take care of that, > but still I am using more disk space than I should. I am open for suggestions > as usual. > -- > ===========================+======================================= > | Mauricio Tavares | "We will attack... |
> | | Rimmer | > ===========================+=======================================
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
John Winte #5 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Quote: > I am finding some very interesting behavior from #define and >#ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This >file is included by all my routines (they usually are in separate files). Now >that would not be very nice since it would include the very same include files >(stdio.h and others) over and over again. So, I thought if I use something >like >#ifndef MOOSE >#define MOOSE >#include <stdio.h> >#include <X11/Xlib.h> >#endif >the code would only include *one* copy of these two include files. What I am >finding out is that is not the case. Running C programs that have similar >conditional including of header files in gcc (Sparc), Think C (Mac), Borland >TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It >really seems that the compiler simply *forgets* what was defined in the >previous .c file and define them all over again.
Yes, that is true. When you compile a .c source file the compiler has no memory of other source files it may have compiled for you in the past. It wouldn't help if it did remember, because either your compilation units depend on the included files or they don't. If they don't, don't include them. If they do, then they won't compile correctly unless you include them. No amount of pre-processor trickery will overcome this one. Quote: >As a result, my object files become unnecessarily large.
This is surprising. There is no way that the inclusion of a properly written header should increase the size of your object files, no matter how many times you include it. Quote: >Yes, the executable seems to take care of that, but still I am using more >disk space than I should. I am open for suggestions >as usual.
If your problem is simply one of disk space, why not delete the object files after each build? Obviously you're trading off build time against disk usage, but if you're that desperate for disk space you're going to have to make a sacrifice somewhere. John (Follow-ups set.) -- John Winters. Wallingford, Oxon, England.
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
Paul Wolf (M #6 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
If you inspect each of the system files, a unique symbol is usually #defined in each file which you can use in your own includes as follows: #ifndef SYMBOL #include <filewithsymbol.h> #endif
Quote:
> I am finding some very interesting behavior from #define and > #ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This > file is included by all my routines (they usually are in separate files). Now > that would not be very nice since it would include the very same include files > (stdio.h and others) over and over again. So, I thought if I use something > like > #ifndef MOOSE > #define MOOSE > #include <stdio.h> > #include <X11/Xlib.h> > #endif > the code would only include *one* copy of these two include files. What I am > finding out is that is not the case. Running C programs that have similar > conditional including of header files in gcc (Sparc), Think C (Mac), Borland > TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It > really seems that the compiler simply *forgets* what was defined in the > previous .c file and define them all over again. As a result, my object files > become unnecessarily large. Yes, the executable seems to take care of that, > but still I am using more disk space than I should. I am open for suggestions > as usual. > -- > ===========================+======================================= > | Mauricio Tavares | "We will attack... |
> | | Rimmer | > ===========================+=======================================
|
Sat, 12 Jun 1999 03:00:00 GMT |
|
 |
Greg Come #7 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Quote: > I am finding some very interesting behavior from #define and >#ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This >file is included by all my routines (they usually are in separate files). Now >that would not be very nice since it would include the very same include files >(stdio.h and others) over and over again. So, I thought if I use something >like >#ifndef MOOSE >#define MOOSE >#include <stdio.h> >#include <X11/Xlib.h> >#endif >the code would only include *one* copy of these two include files. What I am >finding out is that is not the case. Running C programs that have similar >conditional including of header files in gcc (Sparc), Think C (Mac), Borland >TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It >really seems that the compiler simply *forgets* what was defined in the >previous .c file and define them all over again. As a result, my object files >become unnecessarily large. Yes, the executable seems to take care of that, >but still I am using more disk space than I should. I am open for suggestions >as usual.
The compiler need only see something called a `translation unit' at the point you are talking about. So, yes, indeed, it won't know about what happened or not in the previous .c file. Do note that the extra space you are seeing will be symbol table entries for things like function prototypes and such. So, yes again, if b.c uses printf and c.c uses printf, they will both have information stating their needs for that routine. That's just the way it works in the so-called traditional compiler/linker setup. I guess in a million lines of code app, this can for sure eat up lots of space, but I never particularly thought that I'd call this space unnecessarily large. Especially with just the two headers you refer to. Anyway, you know your machine and its constraints better than I. As such, perhaps the following is best for your situation: [pre]link together the more stable parts of your system. That way, you can keep the source seperate but still gather up some of the .o's into a more conglamorative one). - Greg -- Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214 Producers of Comeau C++ 4.0 front-end pre-release ***WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
|
Mon, 14 Jun 1999 03:00:00 GMT |
|
 |
Ruud van Ga #8 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Quote:
> I am finding some very interesting behavior from #define and >#ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. This >file is included by all my routines (they usually are in separate files). Now >that would not be very nice since it would include the very same include files >(stdio.h and others) over and over again. So, I thought if I use something >like >#ifndef MOOSE >#define MOOSE >#include <stdio.h> >#include <X11/Xlib.h> >#endif
Something somewhere is wrong. Having includes be included multiple times this way would also mean that any #define's in the include file would be forgotten. That will certainly NOT be the case. Other possibility: #ifndef is broke. Better: try #include-ing such a file, then do: #ifdef MOOSE #error Should get here #else #error What is going on here? #endif in your C/C++ source. Let me/us know the results. -- Ruud van Gaal MarketGraph Software & Images; Silicon Graphics, PC (DOS/Win95/NT) & Amiga DoomShell 4.5: http://www.xs4all.nl/~jwkorver DoomShell 5.0: 3D Game Alchemy for DOOM, DOOM II, Heretic and Hexen (book) /\ /__\ / \ Datastructures for the computer-illiterate: the binary tree /____\ ||
|
Mon, 14 Jun 1999 03:00:00 GMT |
|
 |
Greg Come #9 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Quote:
>> I am finding some very interesting behavior from #define and >>#ifndef/#ifdef. Let's say that I have a header file called MyHeader.h. >This >>file is included by all my routines (they usually are in separate files). >Now >>that would not be very nice since it would include the very same include >files >>(stdio.h and others) over and over again. So, I thought if I use something >>like >>#ifndef MOOSE >>#define MOOSE >>#include <stdio.h> >>#include <X11/Xlib.h> >>#endif >Something somewhere is wrong. Having includes be included multiple times this >way would also mean that any #define's in the include file would be >forgotten. That will certainly NOT be the case. >Other possibility: #ifndef is broke.
Nothing is broke, or wrong. In the original post, the author was expecting the ifndef (and other things) to live _across_ translation units (.c files if you will) not the typical scenario of a .c that included a .h which may or may not have already #include'd the same thing. Quote: >Better: try #include-ing such a file, then do: >#ifdef MOOSE >#error Should get here >#else >#error What is going on here? >#endif >in your C/C++ source. >Let me/us know the results.
As per above, this is not relevant to the "problem" This is a good technique for people to know in general though. - Greg -- Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214 Producers of Comeau C++ 4.0 front-end pre-release ***WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
|
Tue, 15 Jun 1999 03:00:00 GMT |
|
 |
Richard Wel #10 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
[ comp.os.ms-windows.programmer.misc added back to newsgroups list ] Quote:
> >As a result, my object files become unnecessarily large. > This is surprising. There is no way that the inclusion of a properly > written header should increase the size of your object files, no matter > how many times you include it.
Yes, there is. Some systems place debugging information in the object file, and that debugging information may contain data on all macros and functions declared in the headers. Opinions expressed herein are my own and may not represent those of my employer.
|
Fri, 18 Jun 1999 03:00:00 GMT |
|
 |
Dann Corbi #11 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
Quote: > [ comp.os.ms-windows.programmer.misc added back to newsgroups list ]
> > >As a result, my object files become unnecessarily large. > > This is surprising. There is no way that the inclusion of a properly > > written header should increase the size of your object files, no matter > > how many times you include it. > Yes, there is. Some systems place debugging information in > the object file, and that debugging information may contain > data on all macros and functions declared in the headers.
He meant well written compilers, not stupid ones. If any are actually doing that, they need to be notified so that they can prepare a bug entry in their database. If I only call puts(), there is no reason whatsoever to include printf() information (for example).
|
Fri, 18 Jun 1999 03:00:00 GMT |
|
 |
James Youngm #12 / 12
|
 #ifndef/#ifdef problems in my header file (conditional header inclusion)
[ ... ] What I am Quote: >finding out is that is not the case. Running C programs that have similar >conditional including of header files in gcc (Sparc), Think C (Mac), Borland >TC++ 3.1 for Windows (PC), and SAS/C (Amiga) results in the same thing. It >really seems that the compiler simply *forgets* what was defined in the >previous .c file and define them all over again.
That's the way it's supposed to work. It's called "separate compilation". Quote: >As a result, my object files >become unnecessarily large. Yes, the executable seems to take care of that, >but still I am using more disk space than I should. I am open for suggestions >as usual.
Make sure you don't put *definitions* or *initialisations* in header files, just *declarations*. -- James Youngman VG Gas Analysis Systems |The trouble with the rat-race Before sending advertising material, read |is, even if you win, you're http://www.law.cornell.edu/uscode/47/227.html|still a rat.
|
Tue, 22 Jun 1999 03:00:00 GMT |
|
|
|