home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / Documentation / Documentation_gerlib.doc < prev    next >
Encoding:
Text File  |  1993-12-12  |  33.4 KB  |  1,200 lines

  1. Because some people asked me how to use gcc with own startup-code
  2. (and how to use gcc with Amiga-only programs), here is my environment
  3. for gcc on the Amiga. Everything falls under GNU-Licence, please read
  4. section 14 for more informations on the copyright.
  5.  
  6.  
  7.  
  8.  
  9.  
  10.              Gandalf's
  11.  
  12.                         GCC-gerlib
  13.  
  14.  
  15.                 Version 0.8b
  16.  
  17.  
  18.                     (Sun Dec 12 16:59:26 1993)
  19.  
  20.  
  21.  
  22.  
  23.  
  24. Now it's time to give some of my knowledge to - YOU.
  25.  
  26. you have: - the gcc 2.3.3 compiler
  27.  
  28.             WARNING: GCC 2.4.5 - Gcc 2.5.4 (and perhaps above)
  29.                      wasn't ported by M.Wild, it has
  30.                      some serious bugs, please don't use it !
  31.  
  32.           - an Amiga (500/600/1000/1200/2000/3000/4000)
  33.             with kick 2.04 or above
  34.           - a harddisc
  35.           - a LOT of ram (about 6? MB or more)
  36.           - knowledge of C/C++
  37.  
  38. you probably have:
  39.  
  40.           - some knowledge about the Amiga
  41.           - some knowledge of assembly language (680x0)
  42.           - a debugger like PowerVisor 1.42 or above
  43.           - an assembler like Devpac
  44.  
  45. you want to:
  46.  
  47.           - write fantastic good programs for our only love
  48.             in C or C++
  49.  
  50.  
  51.          WARNING: this stuff is NOT for beginners!
  52.  
  53.  
  54.          WARNING: this documenatation is just in the beginning.
  55.                   If you have any additions/improvements, write
  56.                   me a message!
  57.  
  58.          WARNING: my English is not the best, but I hope it will
  59.                   be more useful than only a german documentation
  60.  
  61.  
  62.          WARNING: please read the HOLE document before you try
  63.                   to use this package!
  64.  
  65.  
  66.          HINT:    I'm using a tabsize of 4. Please use it, or you
  67.                   will have trouble reading this document
  68.  
  69.  
  70. 1. Introduction
  71. ---------------
  72.  
  73. The GNU-gcc compiler is one of the best compilers available
  74. for the Amiga. It is the only real C++-compiler (except for
  75. M*xon C++, which is (in my opinion) a desaster, but not a compiler).
  76. One problem with Markus Wild's implementation of GCC on the Amiga is
  77. that all executables need the ixemul.library, which is often
  78. unneccessary for small projects.
  79.  
  80. So I decided to write my own
  81.  
  82.          - startup code
  83.  
  84.          - support library (not shared, I mean link-library)
  85.  
  86.  
  87. which I now give to the public, e.g. to you.
  88.  
  89.  
  90.  
  91.  
  92. 1.1 Why?
  93. --------
  94.  
  95. With the gcc v2.3.3 (ported by M. Wild), it is easy to port
  96. programs from UNIX to Amiga. It is NOT made to write short,
  97. Amiga-only programs.
  98.  
  99.  
  100. Here my package tries to help you.
  101.  
  102. I program only for the Amiga, I don't plan to port my programs to
  103. another OS/Computer. All I want is
  104.  
  105.  - a simple (but complete), easy to use, short startup code
  106.  
  107.  - only the necessary support routines (e.g. string-functions)
  108.    I don't need all ANSI-C-functions, often there are equivalent
  109.    or better functions in the OS.
  110.  
  111.  - the ability to write resident programs (even with amiga.lib
  112.    support functions !) (Not fully tested yet, but no errors known)
  113.  
  114.  - the ability to debug programs with a debugger, for e.g. PowerVisor.
  115.    This is NOT possible with any program that uses ixemul.library
  116.  
  117.  - the possibility to code in C++
  118.  
  119.  
  120. What I don't need is
  121.  
  122.  - support of all ANSI-C functions
  123.  
  124.  - UNIX-functions (I only port Unix programs, I don't write new ones)
  125.  
  126.  - ixemul.library for every small program
  127.  
  128.  
  129.  
  130. 1.2 What did I do?
  131. ------------------
  132.  
  133.  - I wrote new startup code in assembler for maximum speed and minimum
  134.    size.
  135.  
  136.  - I disassembled parts of amiga.lib to write real resident programs
  137.  
  138.  - I wrote this text with dozens of hints
  139.  
  140.  - I wrote some C++-specific functions that enable the use of
  141.    C++ in standard programs
  142.  
  143.  - I wrote some functions/classes that makes life easier with C++ on
  144.    the Amiga
  145.  
  146.  - I wrote some C++-example programms that shows some things not mentioned
  147.    here. Please have a look at them!
  148.  
  149.  - I compiled libg++ v2.4 (now you can use real iostreams, printf with
  150.    floating-point numbers etc.)
  151.  
  152.  
  153. There are two parts of gerlibv:
  154.  
  155.  - the user part (all things you need to work with gerlibv)
  156.  
  157.  - the developer part (with all sourcecodes and not needed additional
  158.    files)
  159.  
  160. All parts that are in the developer part are marked with (+) in this
  161. manual
  162.  
  163.  
  164. Here's a brief overview over the supplied files:
  165.  
  166. (file) execute_before_recompile (+) needed if you want to recompile libger.a
  167. (file) makeall (+)              makes everything that can be made
  168. (file) makelibrary (+)          makes new libger.a
  169.  
  170. (dir) Documentation             The documentation over my system is here
  171.                                 (you are reading it...). Also the GNU-
  172.                                 Licence can be found here.
  173.                                 Additional docs in amigaguide-format are
  174.                                     - gcc v2.3.3 / cpp
  175.                                     - libg++ v2.4
  176.                                     - iostream
  177.  
  178. (dir) amiga (+)                 contain
  179. (dir) string (+)                  some
  180. (dir) Bonus (+)                     parts
  181. (dir) gnulib (+)                      of
  182. (dir) not_equal (+)                    libger.a
  183.  
  184. (dir) startup                   contains the new startup-code
  185.  
  186. (dir) amiga_lib (+)             everything you need to recompile libamiga.a
  187.  
  188. (dir) support/mui               contains file that is needed if you want to
  189.                                 recompile
  190.                                 the examples that come along with mui 1.3
  191. (dir)         lib               here libamiga.a libger.a and libgpp
  192.                                 are stored, copy them to lib: to use them
  193.                                 also the startup-code for the compiler is
  194.                                 stored.
  195. (dir)         blib              here base-relative libamiga.a libger.a and
  196.                                 libgpp are stored, copy them to blib: to use
  197. (dir)         unix/local        my dmake-startup-files. Copy them
  198.                                 to gcc:unix/local
  199. (dir)         os-include        some additional files for your os-includes-
  200.                                 directory (also needed for mui-support)
  201.  
  202. (file) examples/DemoSource.cc   a very complete demo-C++-Programm. Please have
  203.                                 a look at it
  204. (dir)           add/            some additional useful classes and functions.
  205.                                 Needed for DemoSource.cc
  206. (dir)           test/           Some test-executables, especially how to
  207.                                 handle startup-things
  208. (dir) libg++/* (+)              a nearly full implemention of GNU libg++ 2.4
  209.  
  210.  
  211.  
  212. 2. Installation
  213. ---------------
  214.  
  215. Some things are different in the include-files distributed with this
  216. package compared to the one form m.wild. So, I decided to use different
  217. directories. You can switch between my environment and the one from
  218. standard gcc using the scriptfile "switchgcc". This scripfile renames
  219. some directories.
  220.  
  221. To install my package, type in the following commands:
  222.  
  223. ;First rename some directories:
  224.  
  225. rename gcc:g++-include gcc:gcc_g++-include
  226. rename gcc:include gcc:gcc_include
  227. rename gcc:lib gcc:gcc_lib
  228. rename gcc:blib gcc:gcc_blib
  229.  
  230. ;and make some new directories
  231.  
  232. makedir gcc:include
  233. makedir gcc:lib
  234. makedir gcc:blib
  235. makedir gcc:g++-include
  236.  
  237. ; copy the old contents of include
  238.  
  239. copy gcc:gcc_include gcc:include ALL
  240.  
  241. ; copy the distributed files in the new directories
  242.  
  243. copy gerlib/support/g++-include gcc:g++-include ALL
  244. copy gerlib/support/include gcc:include ALL
  245. copy gerlib/support/lib gcc:lib ALL
  246. copy gerlib/support/blib gcc:blib ALL
  247. copy gerlib/support/os-include/ gcc:os-include ALL
  248.  
  249. ; copy the startup files
  250.  
  251. copy gerlib/startup/#?.o gcc:lib ALL
  252.  
  253. ; and eventually
  254.  
  255. copy gerlib/support/unix/local local:
  256.  
  257. ; if you want to use my make and dmake config files
  258.  
  259.  
  260.  
  261. 3. What is libger.a and libamiga.a
  262. ----------------------------------------
  263.  
  264. The main result of my work are to linker-libraries:
  265.  
  266.     gcc:lib/libger.a  and  gcc:blib/libger.a
  267.  
  268. In these two libraries all the needed functions you possible need
  269. are stored.
  270.  
  271. The first one is for large-data, the secound one for small(base-relative)
  272. data.
  273.  
  274. With small data only 64k of global data can be accessed. The advantage
  275. is that is that small-data programs are a little bit smaller and faster
  276. than large-data ones. And, what is perhaps even more important, you
  277. can write quite easy residentable programs if you use small-data.
  278. Small-data uses a base-register (a4) to access all global variables.
  279.  
  280. Here is a small example how to call gcc with the right options for large data/
  281. small data and residentable programs. The file that should be compiled is
  282. called 'test.c'.
  283.  
  284. For the complete option-list plese refer to the gcc-documentation (also
  285. stored in the directory you found this text here, AmigaGuide-format).
  286.  
  287. a) Large data:
  288.  
  289.     gcc test.c -c
  290.  
  291.   (leaves test.o)
  292.  
  293.     gcc -o test -lger -lamiga
  294.     ^   ^  ^       ^     ^use library libamiga.a
  295.     |   ---|       |
  296.  name of   |       |---- use library libger.a
  297.  the com-  |
  298.  piler     |
  299.            |
  300.            |
  301.            |
  302.   the name of the executable
  303.   to create. Predefined: a.out
  304.  
  305.  
  306. b) small-data
  307.  
  308.     gcc test.c -c -fbaserel -msmall-code
  309.  
  310.     gcc -o test test.o -lger -lamiga -fbaserel
  311.  
  312.  
  313. c) resident programs
  314.  
  315.     gcc test.c -c -resident
  316.  
  317.     gcc -o test test.o -lbger -lbamiga -resident
  318.  
  319.    Yes, it is so easy to to create a residentable program !
  320.  
  321.  
  322. Here all the important options in an overview:
  323.  
  324.                                   used
  325.                     option        startup-m. gerlib amiga.lib
  326.             ----------------------------------------------------------------
  327. large data:                       crt0.o     -lger  -lamiga (uses gcc:lib/)
  328. small data:-fbaserel -msmall-code bcrt0.o    -lger  -lamiga (uses gcc:blib/)
  329. resident  :      -resident        rcrt0.o    -lger  -lamiga (uses gcc:blib/)
  330.  
  331.  
  332. The same results for the c++-linker-libraries libgpp.a
  333.  
  334. It is not possible to write a program that is partly large data
  335. or partly small data. All modules have to be compiled with
  336. "-resident" or "-fbaserel" if you want to write a residentable
  337. program. Yoo HAVE to link with -resident if you want to have a
  338. residentable program, tough.
  339.  
  340. Two useful options for gcc are "-O" (optimize) and "-O2" (optimize even
  341. more). But be careful, optimization needs some time and memory. So use
  342. them only for your final compilations.
  343.  
  344.  
  345.  
  346. 4. The startup-code
  347. -------------------
  348.  
  349. The startup-code is the code that initializes a lot of things before
  350. the actual C/C++-program gets executed.
  351.  
  352. My startup-code is available in different forms:
  353.  
  354.    crt0.o    - normal startup-code
  355.  
  356.    bcrt0.o   - base-relative startup-code (sometimes faster, but
  357.                only limited global variable-size)
  358.  
  359.    rcrt0.o   - resident base-relative startup code
  360.  
  361.  
  362. The correct one is selected from gcc through the options "-fbaserel" or
  363. "-resident"
  364.  
  365. I didn't wrote auto-detach startup-code, because I don't see any use for
  366. it. Everybody should be able to start a program with
  367.  
  368.    run >NIL: <NIL: SomeProgram
  369.  
  370. I don't see any need for it, but probably I (or you ?) will write
  371. auto-detach startup-code.
  372.  
  373.  
  374.  
  375.  
  376. 4.1 What is done in the startup-code ?
  377. --------------------------------------
  378.  
  379. The startup-code does everything necessary except command-line parsing.
  380. Command-line parsing is not always necessary, so I provide only
  381. demo-source how to obtain good command-line parsing.
  382. The things that are done in the startup-code (look also at the RKRM
  383. Libraries) are:
  384.  
  385.    - handling base-relative and resident specific stuff (if necessary)
  386.  
  387.    - handling of workbench and cli startup
  388.  
  389.    - initializing _SysBase and _DOSBase
  390.  
  391.    - initializing of the FPU if necessary
  392.  
  393.    - giving control to main() with
  394.  
  395.         WB-Startup: argc=0
  396.                     argv pointing to a struct WBStartup
  397.  
  398.         CLI       : argc=length of command line (without program name !)
  399.                     argv=pointer to command line
  400.                     On CLI-startup argv and argc are exactly the a0 and
  401.                     d0 which are passed to your program as command line
  402.                     arguments by DOS
  403.  
  404.      Note: the names argc and argv are probably confusing, as argc is
  405.            now the length of the arguement-string and argv is a pointer
  406.            to a string. The names argc and argv were kept for historical
  407.            reasons only.
  408.  
  409.      if you need standard argc and argv, look at the provided examples
  410.      in test/startup/xxx, they demonstrate how to implement this.
  411.  
  412.    - providing clean exit via the "_exit()"-function.
  413.  
  414.    - initializing stdin and stdout with Input() and Output().
  415.      This means if the program is called via WB they are both zero!
  416.  
  417.  
  418. The startup-code is the same for C and C++. It has to be linked
  419. always as the !!first!! module. (Automatically done by gcc)
  420.  
  421. In bcrt0.o the function geta4() is provided which reloads a4 with
  422. the correct value. It is guaranteed that no other register is altered.
  423.  
  424.  
  425.  
  426. 5. gcc-specific functions
  427. -------------------------
  428.  
  429. To be able to use gcc as a C/C++-Compiler on the Amiga I re-wrote some
  430. functions:
  431.  
  432.   void exit();
  433.   default_new_handler();
  434.   void *__builtin_new();
  435.   void __builtin_delete();
  436.   __set_new_handler();
  437.   void *malloc();
  438.   void free();
  439.   int atexit();
  440.  
  441. and some more.
  442.  
  443. These functions were written with Amiga-OS in mind, so no overhead is created
  444. when using this functions.
  445.  
  446. The C-functions
  447.  
  448. bcmp(), bcopy(), bzero(), ffs(), index(), strchr(), memccpy(), memchr(),
  449. memcmp(), memcpy(), memset(), rindex(), strrchr(), strcasecmp(), stricmp(),
  450. strncasecmp(), strnicmp(), strcat(), strcmp(), strcoll(), strcpy(),
  451. strcspn(), strdup(), strerror(), strlen(), strmode(), strncat(), strncmp(),
  452. strncpy(), strpbrk(), strsep(), strspn(), strstr(), strxfrm(), swab(),
  453. memmove(), strtod(), atof(), atoi(), atol(), bsearch(), labs(), ldexp(),
  454. ldiv(), strtoul(), rand(), radixsort(), heapsort(), abort(), strtol(), qsort()
  455.  
  456. are also provided for convenience (I'm sure this list is not complete :-)).
  457.  
  458. Also some math-functions (mostly for the internal datatypes like float, double,
  459. long long etc.) are provided, I hope they are complete.
  460.  
  461. As I don't use floating point variables ver often, they are not tested very
  462. well.
  463.  
  464.  
  465. 6. Amiga.lib
  466. ------------
  467.  
  468. One of my goals was to be able to produce resident programs with gcc.
  469. This was a little bit complicated with amiga.lib, because some
  470. functions (I mean not the stubs, they can easily be created through
  471. the inline-code) made a direct access to stdin, stdout, SysBase etc.
  472.  
  473. So I disassembled this functions, made them base-relative and thus
  474. useable in resident programs.
  475.  
  476. The baserel amiga.lib is in gcc:blib/libamiga.a
  477.  
  478. While disassembling amiga.lib I discovered a bug in
  479. hunk2gcc or ld. The functions that are not o.k. are:
  480.  
  481.   ArgArrayInit()/ArgArrayDone()
  482.  
  483. They produce, when used from the original provided amiga.lib, a nice
  484. CRASH. So be careful when converting objectcode with hunk2gcc!
  485. The problem are the instructions of the form
  486.  
  487.     jsr    (xxxx,pc)                 (new Motorola syntax)
  488.  
  489. They jump, but not to the correct location.
  490. I did't have the time to check every amiga.lib-module, if you find
  491. other problems, please send me a bug-report.
  492.  
  493. I provide the large-data object-file
  494.  
  495.    ArgArrayInitDone.o
  496.  
  497. for your convenience to have a correct version of them.
  498.  
  499. The easier way is to use the libamiga.a that comes along with this
  500. package. There I removed the faulty module above and replaced it with
  501. a correct one.
  502.  
  503.  
  504.  
  505. 7. Programming in C++
  506. ---------------------
  507.  
  508. The standard os-include-files are not fully compatible with C++.
  509. A standard C++-Header looks like for example
  510. (examples/test/examples_doc/example_dok1.cc):
  511.  
  512. /* "hide" the original class and template, they are keywords in C++ */
  513. #define class _class
  514. #define template _template
  515.  
  516. extern "C" {
  517. void volatile exit (int);
  518. #include <exec/types.h>
  519. /* if you want to use the functions of <clib/alib_stdio_protos.h>, do this: */
  520. #include <clib/alib_stdio_defines.h>
  521. #ifdef __OPTIMIZE__        /* need lot of time & memory, is selected by -O2 */
  522. #include <inline/exec.h>
  523. #include <inline/dos.h>
  524. #else
  525. #include <clib/exec_protos.h>    /* faster and easier to debug */
  526. #include <clib/dos_protos.h>     /* but code is worse */
  527. #include <clib/tagdefines.h>     /* file that has defines for xxxTags-Calls */
  528. #endif
  529. }
  530.  
  531. #undef template
  532. #undef class
  533.  
  534. /* standard C++-Programm */
  535. int main(void)
  536. {
  537.     // do someting useful here
  538.     return 0;
  539. }
  540.  
  541.  
  542. One problem occurres when a libary should be closed:
  543.  
  544. extern "C" {
  545. #include <intuition/intuitionbase.h> /* struct IntuitionBase { ...  }; */
  546. }
  547.  
  548. extern "C" {
  549. IntuitionBase *IntuitionBase;
  550. }
  551.  
  552. int main(void)
  553. {
  554.     if(IntuitionBase=(IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library",37))
  555.     {
  556.         // do someting useful here
  557.  
  558.         CloseLibrary((struct Library *)IntuitionBase);
  559.     }
  560.     return 0;
  561. }
  562.  
  563. The compiler is not able to distinguish between the type "IntuitionBase"
  564. and the variable "IntuitionBase" if it is not used in an definite expression,
  565. so it gives the warning (and following error)
  566.  
  567.     warning: name `IntuitionBase' could be type or expression;
  568.              compiler assuming type
  569.     parse error before `;'
  570.  
  571. in the line with CloseLibrary(). So we can only use clear expressions:
  572.  
  573.     struct Library *dummy=(struct Library *)IntuitionBase;
  574.     CloseLibrary(dummy);
  575.  
  576. This works, here the compiler does what we expect him to do.
  577. (examples/test/examples_doc/example_dok2.cc)
  578.  
  579. If you need no direct access to IntuitionBase, you can avoid this problem
  580. by declaring IntuitionBase as Library *. But don't do this if you want
  581. to use gcc:os-include/inline/xx.h, then you would get lots of errors.
  582.  
  583.  
  584. 7.1 Using Classes
  585. -----------------
  586.  
  587. If you are using classes, be sure you leave the compiler the ability
  588. to call the destructors!
  589.  
  590. Don't write things like
  591.  
  592. int main()
  593. {
  594.     dummyclass du;
  595.  
  596.     exit(0);    // destructor of du is never called !!
  597. }
  598.  
  599. However,
  600.  
  601. int main()
  602. {
  603.  
  604.     {
  605.         dummyclass du;
  606.         //...
  607.  
  608.  
  609.     }    // destructor of du is called here
  610.  
  611.     exit(0);
  612. }
  613.  
  614. is OK.
  615.  
  616. Don't call exit() if you are not sure if all classes are "destructed".
  617. Global destructors are called via exit() (and after all functions that
  618. were remembered to call via atexit()), so the call of the global
  619. destructors is no problem.
  620.  
  621. The easiest way is, however, not to use the exit() function at all,
  622. and return from main with a return(<number>);-statement, this is
  623. better ("cleaner") than a call of exit(<number>);
  624.  
  625. If you want to have a look at some sample code, try to understand
  626. the provided example.
  627.  
  628.  
  629. 8. mathematical functions
  630. -------------------------
  631.  
  632. Data types like float, double, long long etc. cause the compiler to
  633. generate function calls to some functions provided in libger.a.
  634. These functions sometimes need the Amiga mathematical libraries.
  635. So the linker will complain:
  636.  
  637. gcc:lib/libger.a(fixsfsi.o):
  638.      Undefined symbol _MathTransBase referenced from text segment
  639. gcc:lib/libger.a(fixsfsi.o):
  640.      Undefined symbol _MathBase referenced from text segment
  641.  
  642. or something else.
  643.  
  644. There is a solutions for the problem: open the needed libraries 'manually'.
  645.  
  646. e.g. the Program testmath.c compiled with
  647.  
  648.     gcc testmath.o -lger -lamiga
  649.  
  650. int main(void )
  651. {
  652.     float pi=3.14159265;
  653.     float e=2.71;
  654.     return e*pi;
  655. }
  656.  
  657. will result in
  658.  
  659. gcc:lib/libger.a(fixsfsi.o):
  660.         Undefined symbol _MathTransBase referenced from text segment
  661. gcc:lib/libger.a(fixsfsi.o):
  662.         Undefined symbol _MathBase referenced from text segment
  663. gcc:lib/libger.a(mulsf3.o):
  664.         Undefined symbol _MathTransBase referenced from text segment
  665. gcc:lib/libger.a(mulsf3.o):
  666.         Undefined symbol _MathBase referenced from text segment
  667. gcc:lib/libger.a(mulsf3.o):
  668.         Undefined symbol _MathTransBase referenced from text segment
  669.  
  670. Now we try (examples/test/examples_doc/example_dok3.c):
  671.  
  672. #include <exec/types.h>
  673. #include <inline/exec.h>
  674. #include <clib/dos_protos.h>
  675. struct Library *MathTransBase;
  676. struct Library *MathBase;
  677. struct Library *MathIeeeDoubBasBase;
  678. struct Library *MathIeeeDoubTransBase;
  679.  
  680. void close_math(void)
  681. {
  682.     struct Library *lib;
  683.  
  684.     if(MathBase)
  685.         { lib=(struct Library *)MathBase;                CloseLibrary(lib); }
  686.     if(MathTransBase)
  687.         { lib=(struct Library *)MathTransBase;             CloseLibrary(lib); }
  688.     if(MathIeeeDoubBasBase)
  689.         { lib=(struct Library *)MathIeeeDoubBasBase;    CloseLibrary(lib); }
  690.     if(MathIeeeDoubTransBase)
  691.         { lib=(struct Library *)MathIeeeDoubTransBase;    CloseLibrary(lib); }
  692. }
  693.  
  694. BOOL init_math(void)
  695. {
  696.     atexit(close_math);
  697.  
  698.     MathBase              = OpenLibrary((STRPTR)"mathffp.library",0L);
  699.     MathTransBase         = OpenLibrary((STRPTR)"mathtrans.library",0L);
  700.     MathIeeeDoubTransBase = OpenLibrary((STRPTR)"mathieeedoubtrans.library",0L);
  701.     MathIeeeDoubBasBase   = OpenLibrary((STRPTR)"mathieeedoubbas.library",0L);
  702.  
  703.     if(MathBase && MathTransBase && MathIeeeDoubTransBase && MathIeeeDoubBasBase)
  704.         return TRUE;
  705.     else return FALSE;
  706. }
  707.  
  708.  
  709. int main(void )
  710. {
  711.     float pi=3.14159265;
  712.     float e=2.71;
  713.     if(init_math())
  714.     {
  715.         /* now we can calculate something */
  716.  
  717.         return e*pi;    /* will be truncated to an int */
  718.     }
  719.     else
  720.     {
  721.         Printf("Not able to open all math-libraries !\n");
  722.         return 20;
  723.     }
  724. }
  725.  
  726.  
  727.     gcc mathtest.o -o mathtest -lger -lamiga
  728.  
  729. And everything is ok.
  730.  
  731. Because we don't use the ixemul printf/sprintf, we can't use them to
  732. print float/double etc. variables.
  733.  
  734. If you want to use printf/sprintf etc. with floating-point variables,
  735. you can #include <stdio.h> and link with "-lgpp -lger -lamiga",
  736. but your program will get a lot bigger (about 60 kb).
  737.  
  738. As mentioned above, I don't use floats very much, so don't rely on them
  739. beeing correct.
  740.  
  741.  
  742.  
  743. 9. MUI and GCC or how Hook calls can be done using gcc
  744. ------------------------------------------------------
  745.  
  746. It is possible to use gcc to compile MUI-programs. You have to make
  747. only a few changes, especially if you want to use hooks:
  748.  
  749. a)    Use the supplied support/mui/demo.h File instead of the one that
  750.     comes along with MUI 1.3
  751.  
  752. b)    Hooks
  753.  
  754. Gcc lacks one feature that are standard on other Amiga compilers:
  755. register parameters.
  756.  
  757. We can solve this problem in 2 ways:
  758.  
  759.  
  760. b.1) use assembler inline functions to get the result in the registers
  761.  
  762.     (Taken from AppWindow.c and modified)
  763.     Example:
  764.  
  765.     LONG AppMsgFunc(REG(a2) APTR obj, REG(a1) struct AppMessage **x)
  766.     {
  767.     /*register struct Hook *a0 __asm("a0"); */ /* not needed in this function */
  768.       register APTR a2 __asm("a2");
  769.       register struct AppMessage **a1 __asm("a1");
  770.     /*struct Hook *hook = a0; */
  771.  
  772.       APTR object = a2;
  773.       struct AppMessage *amsg = *a1;
  774.  
  775.     /* ... */
  776.     }
  777.  
  778. This works only if you are not using base-relative code!
  779. (GCC has nothing like __saveds that calls geta4() automatically)
  780.  
  781.  
  782. b.2) Using stub functions
  783.  
  784. Standard Hook functions have the ability to provide a high language
  785. entry point (h_SubEntry) and a low-level assembler entry point
  786. (h_Entry).
  787.  
  788. Now we can tell our marveless OS to call first the assembler entry
  789. point (here we can put the registers on the stack, and, if needed,
  790. reload a4) which calls the C-function.
  791.  
  792. I provided two different assembler functions which do just this
  793. for you.
  794.  
  795.  
  796.     (void *) hookEntrya1a2()
  797.  
  798. This variant puts only a2 and a1 on the stack, that means it is
  799. usable for Hooks which need only two parameters (like the example
  800. supplied above)
  801.  
  802.  
  803.     (void *) hookEntry()
  804.  
  805. This variant puts a0, a2 and a1 on the stack. It is needed for
  806. all Hooks which need all parameters (like the example
  807. broker_list_confunc() in MUI-Exchange.c)
  808.  
  809. To get the stubs called, a small patch has to be made in the
  810. definition of the hook. Here is an example:
  811.  
  812. you have to write:
  813.  
  814.     static const struct Hook AppMsgHook =
  815.      { { NULL,NULL },(VOID *)hookEntrya1a2,(VOID *)AppMsgFunc,NULL };
  816.     /*                       ^^^^^^^^^^^^^ call correct Hook-Subentry
  817.          that moves the registers on the stack, provided in libger.a */
  818.  
  819. instead of
  820.  
  821.     static const struct Hook AppMsgHook =
  822.      { { NULL,NULL },(VOID *)AppMsgFunc,NULL,NULL };
  823.  
  824.  
  825. If you want to compile base-relative, you have to use this stubs.
  826. They relode a4 correctly, so you have acces to your global variables.
  827.  
  828.  
  829. b.3) Using Hooks with resident code
  830.  
  831. NOTE: due to a bug in gcc it is not possible to use the inline code
  832. for the calls of the MUIMaster.library together with the switch
  833. -resident or -fbaserel. So these comments are only for completeness.
  834.  
  835. If you want to write "pure" or resident-able programs, things get a
  836. litte more difficult. The problem is that it is not so easy to
  837. get a4, because we have no function geta4() that could tell us
  838. where our global-variable-pointer a4 points to, because a hook
  839. is called from within the intuition or gadtools or whatelse
  840. environment.
  841.  
  842. So we have to store a4 in a Hook BEFORE it gets called.
  843.  
  844. For you, that means that you have to use the stubs
  845.  
  846.     hookEntryRes
  847.     hookEntryResa1a2
  848.  
  849. and store a4 in Hook.h_Data with the supplied function aska4()
  850. (see demo.h) like this:
  851.  
  852.     (from within a function)
  853.  
  854.     struct Hook AppMsgHook = { { NULL,NULL },(VOID *)hookEntryResa1a2,
  855.                                 (VOID *)AppMsgFunc,aska4() };
  856.  
  857.  
  858. If you want to use global Hooks, you have to initialize them BEFORE
  859. they are (possibly) called.
  860.  
  861.     static struct Hook DestructHook = {
  862.         {NULL, NULL},
  863.         (void *)hookEntryRes,
  864.         (void *)DestructFunc, NULL
  865.     };
  866.  
  867.     f()
  868.     {
  869.         DestructHook.h_Data=aska4();
  870.  
  871.         /* now we can use the hook DestructHook */
  872.     }
  873.  
  874.  
  875.  
  876. b.4) Make GCC-things #definable
  877.  
  878. If you want to be able to compile the source with different compilers,
  879. define the variant you are using:
  880.  
  881.     #ifdef __GNUC__    /* use __GNUG__ if you are programming in C++ */
  882.         /* our stub called */
  883.     #else
  884.         /* compiler has regargs */
  885.     #endif
  886.  
  887.  
  888. c) __aligned
  889.  
  890. GCC doesn't support the __aligned keyword. It is most needed for DOS objects
  891. (e.g. FileInfoBlock). They should be on an 4 byte boundary. What you can do
  892. instead is to use the Dos function AllocDosObject().
  893.  
  894.  
  895.  
  896. 10. Provided C-Functions and C++-Classes
  897. ----------------------------------------
  898.  
  899. This part is under development, I don't know if I found all bugs...
  900.  
  901. Directory "examples/Add/normal" (and /baserel)
  902. add.h              the Include-File for the C-Functions
  903. CED_Support.c      Code that makes it easier to communicate with CED
  904. CED_Support.h           Version 3.5+
  905. ced_support.o
  906. cina.cc              A Class that emulates the standard "cin" with
  907. cina.h                Amiga-functions
  908. cina.o
  909. couta.cc          A Class that emulates the standard "cout" with
  910. couta.h                 Amiga-functions
  911. couta.o
  912. editfile.cc          A Class that makes it easier to read a file into memory
  913. editfile.h
  914. editfile.o
  915. GetFileSize1.cc      A C++-Function that returns the length of a file
  916.                     (filename given)
  917. getfilesize1.o
  918. GetFileSize2.cc      A C++-Function that returns the length of a file
  919.                     (BPTR given)
  920. getfilesize2.o
  921. ltostr.cc          A C-function that converts a long to a string (any base)
  922. ltostr.o
  923. makefile          Makefile for all files in this directory
  924. OwnError.cc          A Class that makes it easier to remember errors in classes
  925. OwnError.h
  926. ownerror.o
  927. ShowRequest.c      A C-Function that makes it easy to show a nice Requester
  928. showrequest.o
  929. TextClipSupport.c A C-Functions that makes it easier to read and write strings
  930. textclipsupport.o   to the clipboard
  931.  
  932. Directory "libg++"
  933. etc/ (+)          Some examples compiled with my package. I didn't test
  934.                   if the results are the ones expected.
  935. gperf/ (+)        A program to gererate a perfect hast function. Also
  936.                   only compiled, not checked.
  937. iostream/ (+)     the implementation of the iostreams library with
  938.                   only a few small changes to compile on the amiga
  939. src (+)           the rest of libg++ V2.4 package
  940. make_links (+)    (only used if you want to recompile libg++)
  941. makegpp (+)       (only used if you want to recompile libg++)
  942. AMIGA_README.txt  Some implemention details of my libg++-port
  943. libg++.texi       Documentation to libg++ in ".texi"-format
  944. ger_add (+)       some functions that were needed to compile libg++ on
  945.                   the amiga
  946.  
  947.  
  948.  
  949.  
  950. 11 compiling assembler-files with a standard assembler
  951. ------------------------------------------------------
  952.  
  953. The syntax of the assembler-opcodes is different from the standard
  954. MOTOROLA-conventions on the Amiga. If you find this annoying,
  955. you can use another assembler as well.
  956.  
  957. Add this to your dmake-makefile:
  958.  
  959.    %$O : %.asm
  960.     Genam $< -l -d            # add your favourite assembler here,
  961.     hunk2gcc $(<:b).o         # -l : prodoce linkable objectfile,
  962.     delete $(<:b).o           # -d : produce debugging informations
  963.     rename obj.* $(<:b).o
  964.  
  965. With this you are able to use your own assembler-sourcecode (with extension
  966. .asm) within normal makefiles.
  967.  
  968. An example is provided in b:test/startup/: program Ger_Test uses an .asm-
  969. file
  970.  
  971.  
  972.  
  973. 12 Troubleshooting
  974. ------------------
  975.  
  976. Before writing programs in C/C++ with gcc, please look at the provided
  977. DemoSources in examples and examples/test. There a lot of things are
  978. shown which are not described elsewhere.
  979.  
  980. Please also have a look at the makefiles.
  981.  
  982. If you want to debug you programs, you can use every standard amiga-
  983. debugger, as gcc places standard amiga debug hunks in every executable.
  984. A very good debugger is PowerVisor, I use the Version 1.42.
  985.  
  986. If you want to distribute/sell/publish your programs, strip the debug-
  987. hunks (for example with PowerPacker (HunkLab/Process File...)). So the
  988. files will get shorter and it is harder to disassemble your program.
  989.  
  990. Use tools like Enforcer or Mungwall, they helped me a lot.
  991. One hint for Mungwall: you can use a terminal program like NComm
  992. to see the output, you don't have to run sushi. (Of course, you must have
  993. a modem) To find any memory leek use mungwall like this:
  994.  
  995. run Mungwall TASK <YourProgramName> SNOOP
  996.  
  997. Then all AllocMems and FreeMems (rsp. AllocVec/FreeVec etc.) are shown.
  998.  
  999.  
  1000.  
  1001. 12.1 Known problems/bugs
  1002. ------------------------
  1003.  
  1004. - nm: shows only files in an object file which length is not longer
  1005.   than 16 chars  (nm bug)
  1006.  
  1007. - gcc has problems with the options -resident/-baserel with the file
  1008.   inline/mui_master.h (gcc bug)
  1009.  
  1010. - the linker has a problem with libauto.a, I was not able to recompile
  1011.   it in such a way that the resulting programs would work properly.
  1012.   This seems to be a bug in ld, as, if they are linked manually
  1013.   (not linked as a linker-library), everything works. Because of this,
  1014.   I did not provide any 'libauto.a' or something like that.
  1015.  
  1016.   This also means, if you want to use libgpp.a, you HAVE to link the
  1017.   libraries in the following sequence:
  1018.  
  1019.      -lgpp -lger -lamiga
  1020.  
  1021.  
  1022.  
  1023. 12.2 Possible future improvements
  1024. ---------------------------------
  1025.  
  1026. - split math functions in gnulib/xx/gnulib2.o
  1027.   (would save space in executables that need not all the functions)
  1028.  
  1029. - minimize the standard-includes
  1030.  
  1031. - write a better documentation
  1032.  
  1033. - don't call Input() or Output() if stdin/stdout is used
  1034.   (with #include <clib/alib_stdio_defines.h>)
  1035.  
  1036. - state clearly in all files that they are under the GNU licence
  1037.  
  1038. - use pool-functions for new/delete/malloc/free etc.
  1039.  
  1040. - write german  doc-file
  1041.  
  1042. - clear up the startup-code through an assember-guru
  1043.  
  1044. - delete all files in this distribution that are not needed
  1045.  
  1046. - rewrite gerlib/libg++/ger_add/normal/Unix_readwrite.c
  1047.   (I am sure that there are bugs in it)
  1048.  
  1049.  
  1050.  
  1051. 13.1 If you want to recompile gcc:blib/libger.a (+)
  1052. ---------------------------------------------------
  1053.  
  1054. Hint: sometimes I use the assign g: as a shorthand for gcc:
  1055.  
  1056. All files are provided which are necessary to recompile libger.a.
  1057.  
  1058. Before you recompile the first time, execute the script
  1059. "execute_before_recompile", this will copy the normal .s/.c-files
  1060. in a baserel-directory.
  1061.  
  1062. Then type
  1063.     makelibrary
  1064.  
  1065. and gcc:lib/libger.a / gcc:blib/libbger.a will be recreated and copied
  1066. to lib:
  1067.  
  1068.  
  1069.  
  1070. 13.2 If you want to recompile libamiga.a (+)
  1071. --------------------------------------------
  1072.  
  1073. This is normally not necessary. If you really want to (perhaps
  1074. there is a new amiga.lib out there etc.), be warned. You will
  1075. need a LOT of memory and time. On my Amiga 4000/40 it took
  1076. more than 40 minutes to create gcc:blib/libamiga.a.
  1077.  
  1078. Read the file "amiga_lib/HowToRecompile" for further instructions.
  1079.  
  1080.  
  1081.  
  1082. 13.3 If you want to recompile libgpp.a (+)
  1083. ------------------------------------------
  1084.  
  1085. This is normally not necessary. If you really want to (perhaps
  1086. you found a bug), be warned. You will need a LOT of memory and
  1087. time. On my Amiga 4000/40 it took some hours to create libgpp.a.
  1088.  
  1089.  
  1090. Cd to the directory libg++.
  1091.  
  1092. Only necessary before the first compilation: to make links from
  1093. the normal/xx files to the baserel/xx-files, enter
  1094. "make_links"
  1095.  
  1096. To recompile, enter "libgpp". After some hours or so you get the
  1097. new libgpp.a in gcc:lib and gcc:blib.
  1098.  
  1099.  
  1100.  
  1101. 14. Copyright
  1102. -------------
  1103.  
  1104. This program/documentation/etc. falls under the GNU-Licence.
  1105. Please look at the file COPYING for more informations about the copyright.
  1106. Please read there the sections 11 and 12, they are important...
  1107.  
  1108. It is ok to modify everything here for your own purpose. But please
  1109. don't distribute your modified package. The better solution is to
  1110. write me your improvements, and I will make them available to others.
  1111.  
  1112.  
  1113.  
  1114. 15. Acknowledgements
  1115. --------------------
  1116.  
  1117. The whole work here bases on the port done by Markus M. Wild.
  1118. Thanks a lot! I hope he's not too angry that I took some of
  1119. his sourcecodes from ixemul.library.
  1120.  
  1121. Some people that helped me with the documentation and gave me good
  1122. ideas:
  1123.  
  1124.     - Stefan Hochmuth     (corrected lots of mistakes)
  1125.     - Alexander Görig     (my fido-boss)
  1126.     - Christian Ohlendorf (corrected this documentation)
  1127.     - Stefan Micko        (corrected this documentation)
  1128.     - a unknown gerlib user who told me that the startup code crashes
  1129.       on a plain 68000 :-(
  1130.  
  1131. So, I hope this information makes it easier for you to write programs
  1132. for the Amiga with gcc.
  1133.  
  1134. Sorry for my bad English, my native language is German.
  1135.  
  1136. I used an Amiga 4000/40/14/1,2GB with CED 3.5 to write this
  1137. documentation and all the programs. The software was tested
  1138. with Enforcer and Mungwall (but not full on a plain 68000 or
  1139. a WB less than 3.0)
  1140.  
  1141.  
  1142.  
  1143. 16. How to reach the author
  1144. ---------------------------
  1145.  
  1146. Send ideas, improvements and bugreports to:
  1147.  
  1148.         Gerhard Müller
  1149.         Klarweinstr. 32
  1150.         81247 München
  1151.  
  1152.         Germany
  1153.  
  1154. or
  1155.  
  1156. Fido-Net:
  1157.  
  1158.         Gerhard Mueller at 2:247/3508.38
  1159.                      (this can change in the near future)
  1160.  
  1161. In Germany I can also be reached via Z-Netz:
  1162.  
  1163.         GANDALF@AMC.ZER
  1164.  
  1165. or internet:
  1166.  
  1167.         muellerg@informatik.tu-muenchen.de
  1168.  
  1169. I prefer email.
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175. 17. History
  1176. -----------
  1177.  
  1178.     V0.7 (Mon Sep 27 1993) :
  1179.  
  1180.       first official release
  1181.  
  1182.  
  1183.     V0.8 (Fri Nov 26 1993)
  1184.  
  1185.       added libg++ v2.4
  1186.       added some necessary functions
  1187.       removed some bugs
  1188.       changed layout
  1189.  
  1190.     V0.8b
  1191.  
  1192.       Important bug fix in the startup code, works now unter 68000
  1193.       relinked all executables
  1194.  
  1195.       From now on the distribution is splitted in a user part and a
  1196.       developer part
  1197.  
  1198.       look at the file "Changes" for detailed history
  1199.  
  1200.