[comp.lang.c] problems with globals in more than one file of a library

levy@mtcchi.uucp (2656-Daniel R. Levy(0000000)0000) (07/14/90)

Hi,

I have a situation where I would like several routines in a library to set a
global variable, which may or may not be declared in the program which uses
the library.  I also would like to be able to optionally compile a program
that does reference the global variable with a different version of the
library which does not reference the global variable.

Most C compiler/linker systems I have used (on UNIX systems) honor the
practice of declaring (uninitialized) globals in more than one file, though
the strictly correct usage is to declare the global in only one file and
declare externs in the rest.  My trouble is that I can't take the strictly
correct approach with my requirements above, declaring the global in only one
place.  Since I can't know whether a program calling a routine in the library
is going to declare the global variable, I cannot just declare the variable as
extern in the library routines or there may be link errors.  Also, since I
can't know whether a program using the global will be linked with my special
library (without some pain) I cannot just declare the variable as extern in
the program without similarly risking link errors.

This has caused me some real hassle on a Sun4 running SunOS 4.0.3 (I don't
know how to ask the compiler what version it is and "what" isn't very
helpful).  The use of the non-strictly-correct approach (declaring the
global in all files) SOMETIMES results in the setting of a global in the
library routine not being visible (!) in the program calling it.  Yes, this
is without any optimization in compiling both the library and the program.
The addresses of the supposedly identical globals are wildly different when
this happens.  Changing the declaration in the program to extern "fixes" the
problem but as I mentioned above this is not acceptable as a long term
solution because I'd like to compile that same program with a library that
doesn't declare the global.

Now I could complain to Sun, but first I want to know, is there some evidence
that this usage of multiple declarations of externs is "supported"?

TIA


-- 
 Daniel R. Levy * Memorex Telex * Naperville IL * ..!uunet!tellab5!mtcchi!levy
So far as I can remember, there is not one      ... therefore be ye as shrewd
word in the Gospels in praise of intelligence.  as serpents and harmless as
-- Bertrand Russell [Berkeley UNIX fortune]     doves -- God [Matthew 10:16]

johnb@srchtec.UUCP (John Baldwin) (07/18/90)

In article <1990Jul14.002958.3546@mtcchi.uucp> levy@mtcchi.UUCP
(Daniel R. Levy) writes:
>............  I also would like to be able to optionally compile a program
>that does reference the global variable with a different version of the
>library which does not reference the global variable.

Hunh?  Maybe I need to understand this a little better first: what are you
trying to do here?  Is it that you have a program which already defines
the global variable and you want it to work with two libraries, one which
both defines and references a global of the same name, and another lib which
neither defines nor references the global?