problems with globals in more than one file of a library

2656-Daniel R. Levy00000000000 levy at mtcchi.uucp
Sat Jul 14 10:29:58 AEST 1990


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]



More information about the Comp.lang.c mailing list