QB CULT MAGAZINE - Issue 3 - May 2000             
                            BUILDING A LIBRARY
Writer: Chris Charabaruk
Instead of having some dumb ole introduction, I am going to cut to the chase
here regarding making your own library. I'm sure you've thought of making
something to compete with Future.Library or DirectQB, but for this article,
I won't be dealing with making a library like those. Instead, I will describe
to you how to create a library with your own routines or with other people's
OBJ files.
A library is really nothing except a bunch of OBJ files each with one or more
routines (read SUBs or FUNCTIONs) inside, none of which are called main
(main is only found in executable files). Let's not discuss libraries for a
minute though, I want to talk about these routines. In languages like C or
C++, everything is in a routine, even the actual program. You see, when the
operating system runs an executable file, it actually calls the main()
routine inside the program. main() is like a FUNCTION, because it takes in
an array of characters (a string) and returns an integer value. In C/C++ you
write the main() routine like this:
int main()
// code goes here
return 0
Each QuickBasic program that's compiled has this main() routine as well,
except that by just using QB, you can't return an "errorcode" (the value that
main() returns). There is a way, however, but I won't be telling you about
it here. A SUB in QuickBasic is similar to a FUNCTION when written in C/C++,
it looks like this:
void mysub()
// code goes here
The "void" means that the routine does not return a value, unlike the main()
routine above which returns an integer with value 0. Since mysub() is void,
you can forget about putting in "return", or you can add "return null" which
says to the compiler, return nothing. Now that we have covered routines,
now we can go on to OBJ files.
An OBJ (object) file is nothing more than one or more routines ready to be
put in a library or linked to make an executable file. When you use
QuickBasic's compiler at the command line, all that it does is generate a
bunch of OBJ files. Then, you have to use the LINK program to turn them into
an executable, or LIB to make them into a library. If you use the options in
QB or QBX's run menu, it does the entire process of compiling and
linking/libbing for you, but you have less control over it all. I generally
use the command prompt or a makefile to do the job for me.
So you have the routines, you've used the compiler to make the OBJ files (or
you've found some OBJ files that you know have some useful stuff in them),
now how to make it into a library you can use in QuickBasic? Well, actually,
to make a library for QB, you have to use LINK as well as LIB. You need LINK
to make the QLB file for running the program that needs the routines inside
the IDE and the LIB file for compiling. So let's look at how to make this
LIB, according to it's online documentation that came with MASM 6.11, is to
create and to maintain libraries of compiled or assembled object modules.
LINK, on the other hand, is to create an executable program out of these OBJ
files. So how does that help us make a QLB file? Well, the version of LINK
that comes with QB4.5 and all of Microsoft's PDS packages have a /Q switch
which tells LINK to make a QLB file instead of an EXE. So say that your
routines are in the object files mysubs.obj and myfuncs.obj, to make a
QuickLibrary named mylib.qlb, use this line at the command prompt:
LINK /Q mysubs.obj myfuncs.obj, mylib.qlb,,qbqlb.lib;
Or if you use QBX/PDS use this instead:
LINK /Q mysubs.obj myfuncs.obj, mylib.qlb,,qbxqlb.lib;
That makes the QLB file. To make the normal LIB file, you use LIB like this:
LIB mylib.lib + mysubs.obj + myfuncs.obj
So now you have your library. But later on, you have some more routines
(new.obj) you want to add. So rename your mylib.lib and mylib.qlb files to
myoldlib.lib and myoldlib.qlb, and do this to rebuild the QLB:
LINK /Q new.obj myoldlib.lib, mylib.qlb,,qbqlb.lib;
Or for QBX:
LINK /Q new.obj myoldlib.lib, mylib.qlb,,qbxqlb.lib;
To rebuild the LIB file:
LIB mylib.lib + new.obj + myoldlib.lib
So there you go. There's one more topic that I want to mention though,
granularity. When you use a library, all the routines that share an OBJ with
the ones you were using are included in the program. It's better to have each
routine in it's own file because this way, only what is used from the library
is incorporated into your program. On the other hand, if a routine in your
library needs to call another included routine, then it's best to have them
share a BAS file, and therefore a OBJ file.
Well, that's the basics of making your own library. If I was too confusing,
then just email me at evilbeaver.picksoft@zext.net and I will help you
further if I can.