home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_BAS / PBC23A.ZIP / LIB_BI.DOC < prev    next >
Text File  |  1993-10-27  |  25KB  |  545 lines

  1.      LIB_BI.DOC  Copyright (c) 1992  Daniel M. Smith, Jr.
  2.  
  3.               TOWARD LIBRARIES AND INCLUDE FILES
  4.  
  5.  
  6.  
  7. This document consists of a basic tutorial on BASIC include
  8. files and libraries. It is included with my libraries by the
  9. kind permission of the author. The text has been edited for
  10. consistency with my existing documentation and includes minor
  11. alterations where I considered them helpful.
  12.    -- Thomas G. Hanlin III
  13.  
  14. While the following might be considered mundane by experienced
  15. programmers, it's always nice to have a good foundation to begin
  16. building on. With that in mind, the examples and discussion of
  17. topics will be in relatively layman terminology. To give you an
  18. excellent grasp of each idea presented, examples and
  19. step-by-step procedures will be given. If you are using any
  20. version of QuickBASIC including QBX Professional Development
  21. System, the information presented here is applicable to all,
  22. although some versions may present specific problems regarding
  23. arguments and syntax. In fact, it is germaine to all higher
  24. level languages but specifically to the many forms of BASIC
  25. which is the language we are concerned about.
  26.  
  27. Many routines have been developed by programmers to accomplish
  28. tasks within the BASIC environment. Some are simple, yet others
  29. are extremely complex, possibly requiring memory allocation,
  30. etc. Since these routines are already available why waste time
  31. trying to re-invent the wheel so to speak; include the ones you
  32. require into your program with the $INCLUDE Metacommand (hardly
  33. layman language, so let's take time right now to find out about
  34. this special terminology).
  35.  
  36. Metacommands are special commands that are used to make your
  37. programs perform in a specific fashion. BASIC has three such
  38. commands; two ($DYNAMIC and $STATIC) which deal with allocation
  39. of dimensioned arrays and the $INCLUDE command which provides a
  40. means of incorporating external programs or routines into your
  41. programs. It specifically tells the compiler to stop processing
  42. the current program in favor of the program directed by the
  43. $INCLUDE Metacommand. When the included program ends, execution
  44. returns to the next line of the main program following the
  45. included routine.
  46.  
  47. We are only concerned here with $INCLUDE since it is the means
  48. by which all external routines are usually made a part of the
  49. main program. There is a specific syntax required with all
  50. metacommands that needs to be emphasized; most curious is that
  51. these commands are always preceded by REM or the apostrophe
  52. character. The following is the correct syntax for $INCLUDE:
  53.  
  54.    REM $INCLUDE: 'MyFile.BI'
  55.       or
  56.    ' $INCLUDE: 'MyFile.BI'
  57.  
  58. More than one metacommand can be placed on the same line as long
  59. as they are separated by white space. The other two metacommands
  60. don't require an argument, so no colon (delimiter) is required
  61. when using them. Note that the above argument is enclosed by
  62. single quotation marks (apostrophes).
  63.  
  64. What is a ".BI" file? Nothing can be more frustrating than
  65. trying to figure out what constitutes an include file when you
  66. have never been near one before. Forget trying to track it down
  67. in reference books!! Here it is in a nutshell and will save you
  68. a lot of time.
  69.  
  70. First, it doesn't have to have the ".BI" extension at all, but
  71. that's the common extension used for BASIC Include files.
  72. Second, no SUB or FUNCTION programming statements are permitted
  73. in the file; and Third, include files must be in ASCII format. A
  74. ".BI" file then is simply the declaration statements identifying
  75. the external subroutine or function that you want to be included
  76. into the main program. The following syntax is what you could
  77. expect to find in a typical ".BI" file. Let's call it
  78. 'WINDO.BI'.
  79.  
  80. The metacommand is:
  81.    REM $INCLUDE: 'WINDO.BI'
  82.  
  83. The contents of the file might be:
  84.  
  85.    DECLARE SUB Windo (TRow%, LCol%, BRow%, RCol%)
  86.    DECLARE SUB Colour (ForeGrd%, BackGrd%, Scrn%)
  87.    DECLARE SUB Border (Style%, Title$)
  88.    DECLARE SUB WritWin (FC%, BG%, Text$)
  89.    ' (etc)
  90.  
  91. The list could go on to include other windowing capabilities
  92. such as saving the screen the window pops up on, restoring the
  93. screen when the window is closed, etc. Whatever the routine or
  94. function you wish to include in your program requires a
  95. definition statement similar to the above in the ".BI" file. The
  96. actual program defined is, of course, located in a library of
  97. subroutines; you are only telling the program to expect these
  98. particular subroutines to be called elsewhere in the program. If
  99. it were not for the ".BI" file and the declarations contained
  100. therein, when the program reached the call to the subroutine a
  101. "SUBPROGRAM NOT DEFINED" error message would be encountered.
  102. Every subroutine and function must be defined either at the
  103. beginning of the main module or in the ".BI" file if it is to be
  104. called during program execution. I should also point out here
  105. that all arguments (ie., those within the parentheses in the
  106. examples above) must be established in your program before you
  107. make the call to the external subroutine. The % arguments
  108. require integers and $ arguments require strings.
  109.  
  110. Let's assume you have a key trapped that directs your program to
  111. a label called WIN. We would expect to find something like the
  112. following:
  113.  
  114.    WIN:
  115.       TRow% = 8: LCol% = 20: BRow% = 18: RCol% = 60
  116.       BackGrd% = 2: ForeGrd% = 15: Scrn% = 0
  117.       Style% = 1: Title$ = "TEST WINDOW"
  118.  
  119. Now that the parameters are established:
  120.  
  121.    Border Style%, Title$
  122.    Colour ForeGrd%, BackGrd%, Scrn%
  123.    Windo TRow%, LCol%, BRow%, RCol%
  124.  
  125. The parameters can be set when the function is used, for
  126. example:
  127.  
  128.    LOCATE 10, 25
  129.    WritWin 14, 4, "This is only a test!"
  130.  
  131. By the way, why use Colour instead of Color, or Windo instead of
  132. Window? Because COLOR and WINDOW are BASIC keywords! We can't
  133. use them, since they are already defined to mean something else.
  134. Sometimes you have to be careful with names.
  135.  
  136. Declaration is not required for GOSUB, since you never have to
  137. pass any arguments explicitly. The GOSUB statement is kind of a
  138. specialized version of GOTO, not a subroutine in the sense that
  139. a SUB or FUNCTION is.
  140.  
  141. Hopefully, this will help you to a proper perspective of ".BI"
  142. files.
  143.  
  144. NOW!! Here comes the big one. We will step through this very
  145. slowly because libraries are the backbone of programming. Each
  146. time a programmer prepares a method to accomplish a certain job,
  147. it becomes candidate for retention, since the same routine might
  148. be required again (either by himself or other persons who
  149. program in the same language). These routines are normally
  150. placed in libraries. It could be a small library, or, if many
  151. routines have been created, it could be quite large. The term
  152. "library" is very appropriate, because (like books) routines can
  153. be removed from programming libraries and used when you need
  154. them, but do not need to be kept in your program when you don't.
  155. Much work and research go into solving specific tasks or coming
  156. up with better and more efficient ways of doing a particular
  157. job. Making these great routines available to others makes
  158. programming so much easier because you won't have to waste time
  159. trying to come up with the same routine again. Just extract it
  160. from the provided library and place in a new library you are
  161. going to use. Very easily said, but it takes considerable doing.
  162. This is exactly what we are going to examine now.
  163.  
  164. We will step through the entire procedure for creating a new
  165. library exactly in the same order as you should every time you
  166. decide to use someone else's library routines. Please keep in
  167. mind that these instructions are given from a QuickBASIC
  168. viewpoint. With other versions of BASIC you will have to make
  169. substitutions or modifications in the syntax where required.
  170.  
  171. First you must decide which routines you are going to need from
  172. the library. Make a list of the names of each routine on paper
  173. leaving room on the right for additional information. You will
  174. realize the advantage of doing this shortly.
  175.  
  176. Next, you must have a listing (.LST) file of the library to find
  177. out the specific module file the routine is located in. When
  178. .OBJ files are placed in libraries they no longer have an
  179. extension associated with them; they are merely modules within
  180. the library. However, when they are extracted (as we will see
  181. later) they are given the .OBJ extension once again by LIB.EXE
  182. which is the default. Programmers often place related routines
  183. in one module file within the library. If a routine exists in
  184. its own module file then extracting that module file will
  185. provide you with that particular routine; however if several
  186. routines exist in one module file you can't extract a specific
  187. routine you must extract the entire module file. Therefore, just
  188. because we know the name of a routine does not necessarily give
  189. us access to that routine because it could be in a module file
  190. with an entirely different name. When you see your first listing
  191. file you will more readily discern what I mean. If the routines
  192. are contained in one library and the programmer has provided
  193. both ".QLB" and ".LIB" files you will not need a list file if
  194. you use the programmers libraries. Otherwise, if no listing
  195. (.LST) file exists for the library it will be necessary to
  196. create one.
  197.  
  198. That brings us to our next step, creating a .LST file.
  199.  
  200. LIB.EXE is the library management tool that comes with most of
  201. the versions of BASIC that include a compiler. Some programmer's
  202. huge libraries may be too large for some older versions of
  203. LIB.EXE and if so the programmer will make you aware of that in
  204. his readme file or other documentation. QuickBASIC 4.5 and
  205. PDS/QBX 7.x seem quite up to tackling all library tasks. To
  206. create the .LST file enter:
  207.  
  208.    LIB LIBRARY.LIB
  209.  
  210. where LIBRARY.LIB is the name of the library requiring the
  211. listing file. The .LIB extension is not really required, since
  212. LIB.EXE knows it will be working with a .LIB file. NOTE:
  213. LIBRARY.LIB is always replaced with the actual name of a library
  214. in the following examples; xx in a QB .LIB file name is replaced
  215. with the actual number of your version.
  216.  
  217. LIB.EXE and the library must be in the same directory. To make
  218. everything simple I usually create a special subdirectory for
  219. the library and copy LIB.EXE to it along with the library files
  220. I intend to use. Also copy LINK.EXE to the same subdirectory for
  221. future use. There will be quite a bit of activity in this
  222. directory; this will keep everything a little organized. If
  223. something goes wrong it's easy to identify the erroneous files
  224. and start over again without having to search for the files we
  225. created and maybe erase something inadvertantly.
  226.  
  227. After pressing enter in example above you will see the
  228. following:
  229.  
  230.    Operations:        press Enter to continue
  231.       Since we did not define any operation on the command line
  232.       LIB is prompting for direction; none is required at this
  233.       time.
  234.  
  235. Next you see:
  236.  
  237.    List file:         type LIBRARY.LST and press Enter
  238.       You can name the list file anything you wish but it would
  239.       be advisable to name it the same as the library to avoid
  240.       confusion.
  241.  
  242. The .LST file will be created in the current subdirectory. You
  243. can use any text editor to view the file. It may be quite long
  244. so I would not use the DOS TYPE command except to redirect it to
  245. the printer. Make sure you have sufficient paper and that the
  246. printer is on and ready. To redirect the list file to printer,
  247. type the following:
  248.  
  249.    TYPE LIBRARY.LST >PRN
  250.  
  251. List files are in two columns. Each column has two text entries
  252. divided by dots. The left entry is the subroutine name; the
  253. right is the module file to which it belongs. Some module file
  254. names are really quite cryptic; also as mentioned before some
  255. have the same name as the subroutine. Related routines will
  256. probably be in a single module file.
  257.  
  258. Using the example .BI file we looked at before, you might see
  259. this in the listing file of the library containing those
  260. subroutines.
  261.  
  262.    WINDO...............wnd
  263.    COLOR...............wnd
  264.    BORDER..............wnd
  265.    WRITWIN..........writwn
  266.  
  267. Please note that all of the above could have been in the same
  268. module or in all different modules. Notice also that the module
  269. file "writwn" is not spelled the same as the subroutine.
  270. Programmers may change the name of .OBJ files, sometimes just by
  271. abbreviating the name of the routine.
  272.  
  273. Now that we have the .LST file we can really get down to work.
  274. First we must select the routines that will be needed for
  275. inclusion into the main program and identify the module(s) to
  276. which they belong. We will continue to use the WINDO example
  277. throughout this instruction.
  278.  
  279. The next step is to copy the module files from the Library. The
  280. .LST file has shown us the names of the module files we need to
  281. make our imaginary window. We need to extract WND and WRITWN.
  282. Recall that when they are extracted they will be given the .OBJ
  283. extension by default.
  284.  
  285. LIB.EXE can do this. Just type the following:
  286.  
  287.    LIB LIBRARY.LIB  *WND.OBJ *WRITWN.OBJ
  288.  
  289. The "*" tells LIB to copy the module files only. The original
  290. modules will remain in the library.
  291.  
  292. There are several operators that tell LIB what to do and I will
  293. briefly describe some here:
  294.  
  295.    +   preceding the .OBJ file means add to library
  296.    -   preceding a module name removes it
  297.    -+  preceding a module removes original and replaces it with
  298.        the updated version. Be sure the new version is
  299.        available to LIB because removal action is always first
  300.        in order.
  301.  
  302. There are more but the above gives ample ability to manipulate
  303. libraries. Consult your manual on others.
  304.  
  305. After doing the above example, you would have all of the
  306. required .OBJ files in the current subdirectory. You could
  307. verify their existence by typing DIR at the DOS prompt. When
  308. there are more than a few .OBJ files, it may help to keep a
  309. notepad handy to keep track of them.
  310.  
  311. If we needed .OBJ files from a second library we would again use
  312. LIB.EXE to extract those files from that particular library.
  313. There would then be .OBJ files from two different libraries;
  314. these can be easily combined into a new library specific to the
  315. needs of the main program.
  316.  
  317. As mentioned before, QuickBASIC uses two types of Library files:
  318. the normal .LIB files at linking time and .QLB (QuickLibrary)
  319. files when programming in the QuickBASIC environment. Certainly
  320. you want to see your program run, if possible, prior to
  321. compiling an .EXE file so all bugs can be removed. Notice I said
  322. "if possible". There may be occasions when running a program is
  323. not possible because of conflict with the environment. If this
  324. should happen, you will have to create the .EXE file from the
  325. DOS command line or determine the cause of the conflict and
  326. correct it. Refer to your QuickBASIC guide book for the proper
  327. syntax for BC and LINK. I will also describe them briefly after
  328. we create these new libraries. If you do run into this type
  329. situation I suggest you get the rest of your program functioning
  330. the way it should; then make the .EXE file to check the end
  331. result. Then you can work with the sticky subroutine; any error
  332. subsequent to that would definitely suggest the problem is with
  333. that subroutine.
  334.  
  335. So, a .QLB library is the first one we need to see how the
  336. program will function. LINK.EXE will provide the means to do
  337. this job. LINK.EXE has a special switch (/Q) which tells the
  338. program to make the .QLB library. If the switch is omitted
  339. QuickBASIC will not be able to load the library when instructed
  340. to do so with the /L switch because of improper format.
  341.  
  342. The following syntax for LINK will provide a .QLB library:
  343.  
  344.    LINK /Q WND.OBJ WRITWN.OBJ, MYNEW.QLB,,BQLBxx.LIB;
  345.  
  346. LINK.EXE will combine the .OBJ files into the new .QLB library
  347. using QuickBASICs provided library for support routines.
  348. Naturally if you were using some other version of BASIC you
  349. would use the library provided to support the specific
  350. environmental library. NOTE: When using the /L swith to load
  351. QuickBASIC only one .QLB file can be specified. This means one
  352. thing, ALL external routines must be in the same .QLB library.
  353. Also, .QLB libraries are not manageable as .LIB libraries are,
  354. therefore it is important to have identified all routines
  355. required prior to creating the .QLB library. If you forget one
  356. you will have to LINK all .OBJ files again to include the one(s)
  357. you forgot. I should mention that it is possible to do these
  358. tasks within the QuickBASIC environment in which case .QLB
  359. libraries can be manipulated but only by changing the resulting
  360. library to a different name which basically means you are
  361. redoing everything again anyway. An advantage of this method is
  362. that the parallel .LIB file will be created at the same time.
  363. Consult your manual if you opt for this method. It is desirable
  364. to know how to do it from the command line.
  365.  
  366. You should copy the .BI and .QLB file now to your program
  367. directory. The following syntax will make your new library
  368. available to the main program when you are ready to try the
  369. external routines. It is assumed of course that you have entered
  370. the $INCLUDE metacommand into your program (preferably ahead of
  371. other DECLARE statements) and that subroutine arguments have
  372. been entered or will be entered before the call is made to a
  373. particular routine:
  374.  
  375.    QB /L MYNEW.QLB
  376.  
  377. or, if you wish to load the program at the same time:
  378.  
  379.    QB YOURPROG /L MYNEW.QLB
  380.  
  381.  
  382. Your program is up and running now and you have presumably
  383. ironed out all the bugs and are ready to create the .EXE file.
  384. Whether it is a standalone executable or one requiring the run
  385. library your external routines must be supported by parallel
  386. .LIB files. These parallel files contain the same object code as
  387. the .QLB files but in entirely different format. If, when
  388. creating the .EXE file, these files are not found they will be
  389. omitted from the .EXE program. Hence a certain function may not
  390. work at all even though the rest of the program is perfect. For
  391. this reason it is always a very good idea to create the .LIB
  392. file at the same time you create the .QLB file. For purposes of
  393. illustration I will repeat the syntax for creating the .QLB file
  394. so that you may see both of these commands together and their
  395. relationship to each other:
  396.  
  397.    LINK /Q WND.OBJ WRITWN.OBJ, MYNEW.QLB,,BQLBxx.LIB;
  398.  
  399. and here is the command to create the parallel .LIB file:
  400.  
  401.    LIB MYNEW.LIB+WND.OBJ+WRITWN.OBJ;
  402.  
  403. LIB will create MYNEW.LIB in the current directory as the
  404. parallel file of MYNEW.QLB. It should be copied also to your
  405. program directory when it is created along with the .BI and .QLB
  406. files.
  407.  
  408. The semicolon at the end of these commands tells LIB that no
  409. other directions are necessary. Omitting the semicolon will
  410. cause LIB to prompt you for additional information. Try it if
  411. you're curious. If you type LIB alone on the command line the
  412. program will prompt you for each entry. You are allowed 127
  413. characters on the command line, so if you have a considerable
  414. number of .OBJ files to list, using LIB alone on the command
  415. line is advisable, since you are not limited to the number of
  416. .OBJ files you can list. When you run out of space on the first
  417. line, just type an ampersand (&) at the end of the line and
  418. press Enter. The Operations prompt will be repeated and you can
  419. continue entering information.
  420.  
  421. You can now create the .EXE version of your program. From within
  422. the QuickBASIC environment you determine the type of executable
  423. program you desire. BC will compile your program using defaults
  424. based on it's analysis of the program and automatically enter
  425. the LINK phase. If you choose an executable requiring the run
  426. time library LINK will use BRUNxx.LIB in conjunction with your
  427. .LIB file(s) to create the .EXE file. Advantages are smaller
  428. code which may be desirable with larger programs; disadvantages
  429. are slower execution and BRUNxx.LIB must be in the same
  430. directory or available to the program to provide support
  431. routines. If you choose the standalone executable version LINK
  432. will use BCOMxx.LIB and your .LIB file(s) to create the .EXE
  433. file. Standalone executables are larger but run faster and are
  434. self- supporting. Additionally, using command line compiling and
  435. linking, certain options can be used to decrease program size
  436. depending on the requirements of your program. You must consult
  437. your manual for this information; they are usually .OBJ files
  438. that can be linked with your program to curtail specific
  439. unnecessary functions. To use these you MUST link from the
  440. command line.
  441.  
  442. As I mentioned above, sometimes it may be necessary to create
  443. the .EXE file from the command line. In earlier versions of
  444. QuickBASIC it was advisable to do so since smaller .EXE files
  445. could be obtained. Version 4.5 seems to do fine; QBX 7.x is even
  446. better.
  447.  
  448. First you must invoke the QuickBASIC Compiler (BC) to compile
  449. the program. The default syntax for QuickBASIC 4.x follows:
  450.  
  451. For .EXE requiring run time support:
  452.  
  453.    BC YOURPROG.BAS;
  454.  
  455. For .EXE as a standalone program:
  456.  
  457.    BC YOURPROG.BAS/O;
  458.  
  459. You will need to add /E or /V if you use ON ERROR in your
  460. program-- BC will tell you if you forget. Add /C:512 if you use
  461. communications, to set the comm buffer size.
  462.  
  463. There are alternative methods which may be used and they are:
  464.  
  465.    BC     (and answer the following prompts:
  466.  
  467.       Source Filename: [.BAS]: YOURPROG.BAS
  468.       Object Filename: [YOURPROG.OBJ]:   press Enter
  469.       Source Listing: [NUL.LST]:    press Enter
  470.  
  471. Result: all error messages will be printed on screen and may
  472. scroll out of sight.
  473.  
  474. You may have many object files. If you run out of space on a
  475. line, just type a plus (+) at the end and the prompt will be
  476. repeated on the next line.
  477.  
  478.    BC YOURPROG.BAS;
  479.  
  480. Result: all errors will be printed on screen and may scroll out
  481. of sight.
  482.  
  483.    BC YOURPROG.BAS; >YOURPROG.ERR
  484.  
  485. Result: all errors will be printed to a file in the current
  486. directory with name following ">". Redirection to the printer
  487. would be more functional and would not clutter up the directory
  488. as follows:
  489.  
  490.    BC YOURPROG.BAS; >PRN
  491.  
  492. Result: a printout of the errors which you can refer to while
  493. entering the proper operations which really means in the end you
  494. will be using one of the first two examples to tell the compiler
  495. what to do.
  496.  
  497. It is possible to use the same switches as the compiler. When
  498. you reach your final compilation from within the environment,
  499. use the "MAKE EXE AND EXIT" function. The options used by the QB
  500. environment will remain on the screen. Write them down and use
  501. those options (except the /T) when you must compile from the
  502. command line. This method is not recommended, since QB uses a
  503. lot more options than is usually necessary, which results in
  504. your programs being larger and slower than need be. Still, it's
  505. an easy way to get running in a hurry.
  506.  
  507. NOTE: Do not use /T since it turns off error messages and is
  508. only used by default within the QB environment because all
  509. possible errors have, presumably, been eliminated.
  510.  
  511. When you have successfully created YOURPROG.OBJ you are ready to
  512. use the linker (LINK.EXE). Linking is pretty straightforward as
  513. you will see. The syntax is:
  514.  
  515.    LINK YOURPROG.OBJ/EX;
  516.  
  517. If you have multiple object files and libraries:
  518.  
  519.    LINK YOURPROG.OBJ+ANY.OBJ/EX, YOURPROG.EXE, NUL, MYNEW.LIB;
  520.  
  521. Again you are limited to 127 characters on the command line. It
  522. might be more convenient to just type:
  523.  
  524.    LINK /EX   (the /EX option is desirable for optimization)
  525.  
  526. and answer the following prompts:
  527.  
  528.    Object Modules [.OBJ]: YOURPROG.OBJ+ANY.OBJ+NEXT.OBJ
  529.    Run File [YOURPROG.EXE]:        press Enter if no change
  530.    List File [NUL.MAP]:            press Enter for no map
  531.    Libraries [.LIB]: MYNEW.LIB+ANY.LIB
  532.  
  533. If you run out of room on a line just type "+" at the end of the
  534. line and the specific prompt will be repeated.
  535.  
  536. This was not intended to be a complete documentation of the
  537. special features of LIB.EXE, BC,EXE and LINK.EXE, but rather
  538. specific knowledge about common application of the programs with
  539. regards to BASIC programming and the use of third party
  540. subroutines therein. Knowing how to manipulate libraries is an
  541. important investment of time for anyone considering creating
  542. programs no matter what particular language is used. The
  543. knowledge gained is applicable to all.
  544.  
  545.