From: brianr@tekig1.PEN.TEK.COM (Brian E Rhodefer)
Newsgroups: comp.sys.amiga.programmer
Subject: Re: Does opening libraries require a fixed varname?
Summary: Pretty much.
Date: 17 Dec 92 23:40:38 GMT
Reply-To: brianr@tekig1.pen.tek.com
Followup-To: poster
Distribution: na
Organization: Tektronix, Inc., Beaverton,  OR.

In article <BzDnFz.FM5@usenet.ucs.indiana.edu> shulick@navajo.ucs.indiana.edu writes:
>
>If I wanted, for instance, to open intuition.library, does the line
>*HAVE* to be:
>
>IntuitionBase = OpenLibr.......
>
>Can it be something like "IBase = OpenLibrary(.." or "blech = Open..."
>In other words, does it matter what you call the variable?
>GadToolsBase, ExecBase, GfxBase, etc.
>

You're well advised to stick to the standard names for the library base
variables.  Somehow or other, when you try to *use* an Amiga library
function with a statement like,

 "... myWindow = OpenWindow(blah, blah);",

your compiler has to arrange to call the correct offset in the correct
library, and it's pretty much got to be able to make an assumption about
where to find the library base (seeing as how the library base doesn't
appear in the argument list of the call).  The assumption it makes,
one way or another, is that the library base is cached in the "standard"
variable.

In the early days, my Manx Aztec C ( and I assume Lattice, too)
compiler didn't call the Amiga library directly, but would instead
call a "glue routine" which would copy the arguments off the stack
(i.e., the library routines appeared to be "standard" C subroutines;
therefore, their arguments got shoved on the stack) and into the registers
that the Amiga library package expected, and then called the correct
offset from the library base.  These glue routines needed to use the
correct Library base address, of course, and the way that they GOT the
base address was to declare the base address to be an extern Library *,
and let the linker establish the connection to the library that your
code opened.  Ergo, if you didn't provide a global variable of the exactly
correct name, you'd get an "Undefined symbol" error at link-time.

I don't remember which compiler "invented" the feature first, but both
of them now can generate direct calls on Amiga library functions without
going through a glue routine.  This trick still depends on the compiler's
assumption about where to find the library base address being justified
by your choice of variable name, however.  I only recently switched my
allegiance from Manx to SAS, and haven't got much experience with SAS,
but I recall that the Manx V5.whatever compilers allowed you to do a library
function call with an explicit "libCall(libraryBase,LibraryOffset,arg...)"
statement.  A lot more clutter, though, than just doing
"LibraryFunction(arg...);".

The SAS 6.0 stuff is REALLY slick, though, in that it will automatically
handle the opening and closing of libraries FOR YOU!!!  Apparently, when
the standard c linker-libraries (as distinct from Exec libraries) are
scanned, if the "standard" library base variable names (e.g., IntuitionBase)
are found to be unresolved symbols in your object module, the modules pulled
from the linker library that resolve them also supply OpenLibrary() and
CloseLibrary() initialization/finalization subroutines for them that get
automagically linked into the startup/termination code (the stuff that calls
your "main()").  But, again, in order for this magic to work, you have to
use the "standard" names for things.

Brian Rhodefer