Re: globals
Posted in 1995
>From: gbw@netcom.com >Subject: globals >Date: Mon, 23 Jan 1995 20:03:48 GMT >X-Informix-List-Id: <news.11024> > >Help, > >I have a lot of programs each of which has its own set of >global variables. They all make reference to a set of library >routines which has its own set of global variables. When I >compile one of these programs I need to use 2 sets of globals. > >The informix 4gl books that I have say that a program may have at >most 1 globals block. Is there any way to get around this? Yes. The detailed answer requires a review of some C/Unix concepts which may not be familiar to everyone, so please be patient. In C, a variable can be * defined and initialized in the current file and accessible outside (T1 below) * defined (but not initialized) in the current file and accessible outside (T2) * defined in the current file and not accessible outside (T3 or T4) * declared in the current file and defined outside (T5) These correspond to: Type variable1 = value; /* T1 */ Type variable2; /* T2 */ static Type variable3 = value; /* T3 */ static Type variable4; /* T4 */ extern Type variable5; /* T5 */ When you write in 4GL: GLOBALS "globals.4gl" the variables listed in the file globals.4gl are treated like T5. When you write: GLOBALS DEFINE ... END GLOBALS the variables are generally treated like T2. However, if the I4GL is version 6.00 or later, any DATETIME or INTERVAL variables (and possibly blob variables) defined like this are treated like T1. When you omit the keyword GLOBALS, the variables are treated like T4 (or T3 if the type is DATETIME/INTERVAL/blob and the 4GL is version 6.00 or greater). What does this mean? (1) You can have multiple independent sets of globals referenced by disjoint sets of files, and provide you link all the definitions together with the referencing object files, there will not be any problem. (2) With pre-6.00 compilers, you can have multiple files defining the same variable with explicit GLOBALS DEFINE v ... END GLOBALS, and most C compilers (all that I know of) will let you get away with it. If the 4GL is version 6.00 or later and the variable is of the wrong type, the compiler will object because the variable is multiply defined. (3) If you understand the detailed implications of the mechanism, it is possible to write a globals file which declares all sorts of variables which are actually defined in a variety of other places. Just be very careful to ensure that the types are defined and declared identically. A single module (.4gl file) can only reference a single globals file in all released versions of the software. I expect that 4.14/6.02 will lift this restriction and the preamble to a file will be able to contain an arbitrarily interleaved collection of GLOBALS "file.4gl" statements, GLOBALS DEFINE ... END GLOBALS blocks and DATABASE statements, subject to the constraint that the same database must be referenced in every DATABASE statement. For example, the following will not work in any existing version of 4GL, but should work in 4.14/6.02: DATABASE SomeDB GLOBALS "globals1.4gl" GLOBALS "globals2.4gl" GLOBALS "globals3.4gl" DEFINE ... MAIN -- or FUNCTION or REPORT ... >Is there any way to #include one globals file inside of another? Someone else answered this succinctly and accurately: NOPE And there won't be in 4.14/6.02 either. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h>