delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/18/18:46:46

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: making programs modular
Date: Mon, 18 May 1998 18:26:48 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 56
Message-ID: <3560B5A8.4641@cs.com>
References: <355FA4E1 DOT C3E90E7D AT erols DOT com>
NNTP-Posting-Host: ppp134.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dan Chak wrote:
> 
> I have files bj.cc and options.cc
> I put them both in the project.
> Both files need to include a third file "fxtools.h" which has #ifndef's
> &c.
> Now, when I try to compile, during the linking, it tells me that
> everything defined in fxtools.h is being redefined, even though it has
> #ifndef's.

It would be best if you could post some kind of sample code that
demonstrates your problem.  Generally, in a program with multiple source
modules, you'll create one header file per module, one global header
file for the whole program, or a combination of those two.  Each header
file should contain _declarations_ for any code defined in one module
but used in another, including #defines, struct definitions, typedefs,
external variable declarations, and function prototypes.  Each header
file should then be #included in each module where it is relevant,
including the module(s) from which it is derived.

You don't say specifically what this "fxtools.h" file is, but it should
be #included in any modules that require the code it declares, and there
should be some kind of corresponding source or object module that goes
along with it.  If it contains actual code that is not inlined, then
it's poorly written and should not be used.

> When I used to just include options.h from bj.cc (and have everything
> from options.cc in options.h -- not the greatest programming practice, I
> know..) I would have no problems with linking because the #ifndef's were
> working properly.  Now what's gone wrong?  How do I get around this?

For most modular projects, the only times you'll need to use #ifdef or
#ifndef are to make a header file granular (so that it can only be
#included once per module) or to conditionally compile parts of the code
(as in for debugging).  However, if the header file _defines_ code
instead of declaring it, it doesn't matter how granular it is; if it's
#included in multiple modules you'll get multiple definition errors.

> I thought writing code more modularly was going to make things easier,
> not harder! <grin>

It does, once you get the hang of it.  My best advice is to study the
way other modular programs are written and copy their structure.  I'd be
glad to give you more assistance, but please note that this topic is not
strictly relevant to DJGPP, so if you want to follow up with me you
should do it via email.

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Sex should be friendly.  Otherwise  |
|       aka Fighteer I       | stick to mechanical toys; it's more  |
|   mailto:fighteer AT cs DOT com   | sanitary."                           |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019