🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3647

Re: #ifndef/#ifdef problems in my header file (conditional header inclusion)

#3647
From: John Winters
Date: Tue, 24 Dec 1996 00:00
51 lines
3277 bytes
In article <gnusenetraub-2412960039190001@dialup52.afn.org>,
Mauricio Tavares  <raub@kushana.aero.ufl.edu> wrote:
>        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.

>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, 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.
Technical Details
Message-ID: <59pp6n$1k8@polo.demon.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <gnusenetraub-2412960039190001@dialup52.afn.org>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,157ed6c64d1aa006
X-Google-Attributes: gid11318c,public
X-Google-Thread: 101ec9,8f16387167a7efa5
X-Google-Attributes: gid101ec9,public
X-Google-Thread: 1014db,8f16387167a7efa5
X-Google-Attributes: gid1014db,public
X-Google-Thread: 105df1,8f16387167a7efa5
X-Google-Attributes: gid105df1,public
X-Google-Thread: fc89c,8f16387167a7efa5
X-Google-Attributes: gidfc89c,public
X-Google-Thread: ffc26,8f16387167a7efa5
X-Google-Attributes: gidffc26,public
From: john@polo.demon.co.uk (John Winters)
Subject: Re: #ifndef/#ifdef problems in my header file (conditional header inclusion)
Date: 1996/12/24
Message-ID: <59pp6n$1k8@polo.demon.co.uk>
X-Deja-AN: 205886717
x-nntp-posting-host: polo.demon.co.uk
references: <gnusenetraub-2412960039190001@dialup52.afn.org>
followup-to: comp.lang.c,comp.unix.programmer
organization: Spirit software
newsgroups: comp.unix.programmer,gnu.gcc.help,comp.lang.c,comp.sys.mac.programmer.help,comp.os.ms-windows.programmer.misc,comp.sys.amiga.programmer