home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / !gcc / docs / Intro < prev    next >
Encoding:
Text File  |  2004-10-05  |  17.8 KB  |  483 lines

  1. Introduction to GCC 3.4 for RISC OS
  2. ===================================
  3.  
  4. This is a short introduction to GCC, the GNU Compiler Collection or RISC OS.
  5. This document does not provide an introduction to programming, for that you
  6. will have to get a text book on the language you are interested in.
  7.  
  8. IMPORTANT NOTE 1: The GCC compiler binaries are at least 3000K, so you must
  9. ensure that a large enough Wimpslot has been allocated. Please refer to the
  10. `Troubleshooting' section later in this document, if you are getting
  11. `Address exception' and `No writeable memory at this address' errors.
  12.  
  13. IMPORTANT NOTE 2: If ypu have received this copy of GCC from a CD, then it is
  14. likely there is a more up-to-date version obtainable from elsewhere.
  15.  
  16. IMPORTANT NOTE 3: GCC requires a minimum of 16MB RAM.
  17.  
  18. The RISC OS GNU Compiler Collection consists of a generic front end program
  19. `gcc' that provides a consistent interface to the compilers themselves.
  20. Currently compilers are supported for the languages C, C++ and Fortran.
  21.  
  22. GCC is covered by the FSF General Public License (see the files docs.COPYING
  23. and docs.COPYINGLIB for details).
  24.  
  25. This port of GCC is Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001 Nick Burrett
  26. Copyright (c) 2002, 2003, 2004 GCCSDK Development Team
  27.  
  28. As with all GNU programs, THERE IS NO WARRANTY OF ANY SORT
  29.  
  30. The standard RISC OS distribution contains GCC and supporting binaries
  31. allowing you to start developing applications straight away. So you get
  32. a C compiler, a C++ compiler, an AOF linker, an AOF assembler, the UnixLib
  33. C runtime library and headers and stubs for the SharedCLibrary (as found in
  34. the RISC OS ROMS). 
  35.  
  36.  
  37. Installation
  38. ------------
  39.  
  40. To install GCC, copy the contents of this archive onto your hard disc.
  41. Other compilers can be installed by copying the contents of their
  42. archives into the same directory as where you install GCC - RISC OS will
  43. automatically combine the !gcc directories together. Running !gcc will
  44. setup Run$Path to automatically search for the compiler tools.
  45.  
  46. As compilers are installed, sub-directories named according to the language
  47. they support will be placed within !gcc.docs. These sub-directories will
  48. contain specific information for the language compiler, including its
  49. usage and environment requirements.
  50.  
  51. GCC uses a linker front end, LD. This will search for both drlink and link
  52. on the current Run$Path and in that order.
  53.  
  54. This completes installation, and GCC may be used simply by typing
  55. 'gcc' at the CLI prompt.
  56.  
  57.  
  58. Simple compilations
  59. -------------------
  60.  
  61. This assumes you have actually installed a compiler and are using UnixLib
  62. as the run-time library. Please note that the instructions below are generic
  63. and the ideas presented can be applied to all GNU compilers. The usage of
  64. C source files is merely for example purposes.
  65.  
  66. Source files are stored in the standard ASCII text format and kept in a
  67. directory that is relevant for the language of your program. For example,
  68. C programs are stored in directory 'c' and C++ programs are stored in 'cc'.
  69.  
  70. For simple use, all that is needed is to set up a command line prompt and
  71. ensure there is enough free memory to run the compiler (details of memory
  72. requirements vary between compilers and can be found in the specific
  73. compiler documentation).
  74.  
  75. To compile a C program just type:
  76.  
  77.    *gcc hellow.c
  78.  
  79. This will compile, assemble and link the source 'c.hellow' to produce an
  80. executable called '!RunImage', which can be run immediately.
  81.  
  82. Adding the switch '-o' to the command line, it's possible to specify a
  83. place to store the resultant file:
  84.  
  85.    *gcc hellow.c -o hellow
  86.  
  87. This will comple, assemble and link the source 'hellow.c' to produce an
  88. executable called 'hellow', which can be run immediately.
  89.  
  90. It is possible to compile several sources at one go, mixing C sources in
  91. the same program, using a command line of:
  92.  
  93.    *gcc file1.c file2.c file3.c file4.c
  94.  
  95. Again, this will compile, assemble and link all the sources to produce the
  96. '!RunImage' executable.
  97.  
  98. To compile to the object form, use the -c switch. i.e.
  99.  
  100.    gcc -c file.c
  101.  
  102. will compile and assemble 'file.c' to 'file.o'.
  103.  
  104. To compile the same files but using the SharedCLibrary as the target
  105. run-time library instead of UnixLib, add the switch '-mlibscl' to the
  106. command line. For example:
  107.  
  108.   gcc hellow.c -mlibscl
  109.  
  110. will compile and link the hellow.c source file, using header files
  111. from the SharedCLibrary, and linking against the library C:o.stubs.
  112. The output file will still be called '!RunImage' and can be run
  113. in the same way.
  114.  
  115. Support has been provided for both RISC OS and UNIX format filenames. GCC
  116. will translate any UNIX filenames into their RISC OS equivalent. For example:
  117.  
  118.   ../include/stdio.h    is translated to    ^.include.h.stdio
  119.   /work/gcc/hello.c    is translated to    $.work.gcc.c.hello
  120.   stdlib.h        is translated to    h.stdlib
  121.  
  122. but
  123.   tree.def        is translated to    tree/def
  124.  
  125. For further examples of compilation procedures, please look at `Examples'
  126. in !gcc.files.
  127.  
  128. The full GCC manual can be referred to at !gcc.docs.gcc.
  129.  
  130. Switches
  131. --------
  132.  
  133. This is a list of the simpler, standard switches for GCC. Full details can
  134. be obtained from the !gcc.docs.gcc manual. Please see below for the ARM
  135. specific switches since these have altered from the original gcc version.
  136.  
  137. The switches described below are useable with any GNU compiler.
  138.  
  139. -c              compile and assemble the source to the object file
  140. -S              compile the source to assembler code
  141. -E              preprocess the source file
  142. -o <file>       put the resulting output into <file>
  143. -O              apply some optimisations to the output
  144. -O2             apply full optimisation.
  145. -O3             full optimisation and inline small functions as well.
  146.  
  147. There are some additional ARM specific switches.
  148.  
  149.  -mapcs, -mapcs-frame
  150.    target for ARM Procedure Call Standard stack frames.
  151.  
  152.  -mpoke-function-name
  153.    place the name of the current function before the start of the function
  154.    to allow the post mortem debugger to print a readable backtrace.
  155.    This option is disabled by default.
  156.    Turing this option on can increase code size by about 3.5% for C programs
  157.    and as much as 10% for C++. However, it should be noted that the
  158.    performance of the generated application will not be slower. It is useful
  159.    to turn this option on when developing applications as it can lead to a
  160.    clear indication of where a program is failing.
  161.  
  162.  -mapcs-stack-check
  163.    provide explicit stack checking on entry to each function which allocates
  164.    temporary variables on the stack.  This option is enabled by default.
  165.    Disabling this option will reduce the function prologue overhead by
  166.    3-5 instructions.
  167.  
  168.  -mfpe=2
  169.    Target the Floating Point architecture version 2.  Notably the function
  170.    prologues and epilogues use the STFE instruction to preserve floating-point
  171.    registers rathern than the much faster and more accurate SFM instruction
  172.    of the version 3 (default) architecture.
  173.  
  174. RISC OS specific options
  175.  
  176.  -mthrowback
  177.    send errors to a text editor capable of receiving 'throwbacks'.
  178.    Error throwbacks will only occur when the DDEUtils module has previously
  179.    been loaded.
  180.  
  181.  -mlibscl
  182.    build and link source code against the SharedCLibrary headers and
  183.    library files.
  184.  
  185.  -munixlib
  186.    build and link source code against the UnixLib headers and library
  187.    files. 
  188.  
  189.    -mlibscl and -munixlib are mutually exclusive.  -munixlib is the
  190.    default.
  191.  
  192.  -mamu
  193.    Generate the file !Depend which contains a list of all the source
  194.    files that the produced object depends on. This is intended for use
  195.    with Acorn's Make Utility (AMU), which edits the makefile to include
  196.    these after the '# Dynamic Dependencies' line.
  197.  
  198.  
  199. LD
  200. --
  201.  
  202. LD is a front end program that converts the linker command line as generated
  203. by gcc to a format recognised by Drlink and Link. LD also attempts to
  204. convert Unix format names to RISC OS format. The option recognised are:
  205.  
  206.  -L<dir>
  207.    This specifies a directory/path that LD is to use when searching
  208.    for libraries. <dir> must be either a path (i.e gcc:, etc), or it
  209.    must be a valid Unix/RISC OS directory name, ending in either '/'
  210.    or '.'. Directories are searched in the order that they are
  211.    specified.
  212.  
  213.  -l<file>
  214.    This specifies the name of a library file which LD will search the
  215.    library directories for. This may either be a complete filename (i.e
  216.    C:o.stubs) or just the leaf name, in which case, LD will search
  217.    though the specified library directories for the file o.lib<file>
  218.    and o.<file> if the former isn't found.
  219.  
  220.  -o <file>
  221.    This specifies the filename to which the resultant executable will
  222.    be stored.
  223.  
  224.  -via <file>
  225.    <file> contains a list of object files to be included in the link process.
  226.  
  227.  -version
  228.    Print the version number of 'ld'.
  229.  
  230.  -help
  231.    Display a brief summary of LD and its command line arguments.
  232.  
  233.  
  234. The following Drlink linker commands are recognised:
  235.   -acornmap, -area[map] <file>, -aif, -aof, -bin, -case,
  236.   -leave[weak], -map, -no[unused], -output, -res[can],
  237.   -throwback, -via <file>, -verbose
  238.  
  239. The following Acorn Link linker commands are recognised:
  240.   -aif, -aof, -bin, -map, -via <file>, -nounused[areas], -verbose
  241.  
  242. Unrecognised commands are flagged as an error and LD will not perform the
  243. link process. Characters with square brackets are optional.
  244.  
  245. The sequence '-Lgcc: -LC: -lgcc -lstubs' will cause LD to look for the files
  246. gcc:o.libgcc, gcc:o.gcc, c:o.libgcc, c:o.gcc, gcc:o.libstubs, gcc:o.stubs,
  247. c:o.libstubs and c:o.stubs, taking the first gcc and stubs files that it finds.
  248.  
  249. Linker options can be passed from GCC to link using -Wl and -XLinker options.
  250.  
  251. The linker must be in the default Run$Path, as LD does not handle the -B
  252. command. Please note that it is best to define GCC$Linker in the !gcc.!run
  253. file to point to the linker you want to use. e.g.
  254.   Set GCC$Linker "link"
  255. will make LD use the program 'link' for linking.
  256.  
  257.  
  258. Predefines
  259. ----------
  260.  
  261. The following preprocessor constants are defined by GCC:
  262.    '__arm', '__arm__', '__riscos', '__riscos__', '__aof__'
  263.    '__GNUC__', '__GNUC_MAJOR__', '__JMP_BUF_SIZE'
  264.  
  265. To implement dynamic allocations within a function, it is necessary to add
  266. an extra field to jmpbuf. Fortunately the setjmp.h header files recognise
  267. __JMP_BUF_SIZE and can alter their jmpbuf array appropriately.
  268.  
  269. When targetting the SharedCLibrary (i.e. -mlibscl switch is supplied on
  270. gcc command line) then an additional preprocessor constant is defined,
  271. namely `__TARGET_SCL__'.
  272.  
  273.  
  274. GCC and Virtual Memory
  275. ----------------------
  276.  
  277. GNU compilers use a lot of memory and nothing can be done about that. For
  278. the Risc PC, UnixLib has been modified to make use of dynamic areas. So
  279. users with small memory machines can use Clares' Virtualise to provide a
  280. working virtual memory envionment for them. See the section *Dynamic Areas*
  281. below.
  282.  
  283. Users of older machines, the Archimedes and A5000, with less than 8MB RAM
  284. will not be able to use this software.
  285.  
  286. Dynamic areas
  287. -------------
  288.  
  289. The following information is deprecated, as recent versions of UnixLib
  290. can extend their own Wimpslot, but it is included for reference.
  291.  
  292. UnixLib has a facility to use Dynamic Areas, found in RISC OS 3.5
  293. and above, for the program's heap. The heap is another name for the
  294. malloc area. The advantages of using a dynamic area for the program's
  295. heap include :
  296.  
  297.    * the Wimpslot of the program can be limited to the code and stack
  298.      requirements of the program. This helps alleviate the cost of RISC
  299.      OS task switching which slows down dramatically as the Wimpslot
  300.      grows in size.
  301.  
  302.    * Dynamic Areas provide a means of Virtual Memory when used with a
  303.      product such as Virtualise[1]. Virtual Memory allows programs to
  304.      use more data space than is available with just physical memory.
  305.  
  306. Basically, use
  307.  
  308.   *Set GCC$Heap ""
  309.  
  310. to enable dynamic areas for the compiler and
  311.  
  312.   *UnSet GCC$Heap
  313.  
  314. to disable dynamic areas for the compiler.
  315.  
  316. Before running a program set the wimpslot in the normal way, but it only
  317. needs to be large enough to hold the sum of the largest executables that
  318. can be run concurrently. e.g., with gcc, use the unsqueezed size of gcc
  319. plus cc1. When running from a command line amu remember to allow
  320. additional space for the amu executable and amu's workspace (though this
  321. is tricky to estimate!).
  322.  
  323. If other programs that form part of a compiler are run directly from the
  324. command line, e.g., gnatmake, then separate environment variables must
  325. be set to allow these programs to use dynamic areas for the program's
  326. heap. So, with gnatmake,
  327.  
  328.    *Set gnatmake$heap ""
  329.  
  330. As a user, you should not really need to worry about these details as the
  331. !gcc.!run file already contains the relevant lines. Please read the !run
  332. file if you are a Risc PC user, since some lines will need to be uncommented.
  333.  
  334. [1] Virtualise is a commercial product available from Clares. Peter Burwood
  335. has no connection with Clares or the Virtualise product other than being a
  336. user of Virtualise. If other similar products become available then UnixLib
  337. should work transparently since the UnixLib startup code does not use
  338. any Virtualise features.
  339.  
  340.  
  341. Troubleshooting
  342. ---------------
  343.  
  344. Here are described some common problems users have with gcc and the GNU
  345. compilers. Possible solutions are provided for the known problems.
  346.  
  347. 1. The compiler fails to compile anything but no error message is reported.
  348.  
  349.   This is usually caused by a lack of memory. To see how far through
  350.   compilation a file has gone, try compiling with the '-v' flag. '-v' gives
  351.   verbose output and will show the programs gcc is executing.
  352.   
  353.   A lack of memory will be shown up by an almost instantaneous return from
  354.   any program executed. If this happens, try increasing the wimpslot,
  355.   using *wimpslot -min nnnK, where nnn is as follows:
  356.  
  357.       4000K or more for GNU C, Objective-C, C++, Fortran and Pascal.
  358.       6000K or more for GNAT Ada.
  359.   
  360.   When the compiler runs out of memory during compilation, an error
  361.   'virtual memory exhausted' is usually reported.
  362.  
  363. 2. Abort on data transfer errors are displayed during compilation
  364.  
  365.   Insufficient memory has been allocated to the program. Increase
  366.   the wimpslot.
  367.  
  368. 3. The error 'No writable memory at this address' is returned.
  369.  
  370.   This is caused by insufficient memory memory and can be fixed by
  371.   increasing the wimpslot to similar values as above.
  372.  
  373. 4. ld fatal error: drlink returned exit status 12
  374.  
  375.   This is caused by insufficient memory during the linking stage.
  376.   Increase the wimpslot to fix this.
  377.  
  378. 5. A source file is correctly compiled and linked. The resultant executable
  379.    'hellow' is placed in the current directory. However, when I try and
  380.    run the file I get the error 'No such file or directory'.
  381.    
  382.    This and many other obsure errors are caused by linking source files
  383.    against run-time libraries that are incompatible with the StrongARM
  384.    processor. An example would be UnixLib 3.7a.
  385.    
  386.    StrongARM compatibility wasn't introduced to UnixLib until version 3.7b.
  387.  
  388.  
  389. AOF files, Libraries, Linkers etc.
  390. ----------------------------------
  391.  
  392. The following is a brief introduction to the world of compiling source
  393. code, object files and linking.
  394.  
  395. Taking the C compiler as an example. Language C source files are stored
  396. within a directory called 'c'.
  397.  
  398. Typing
  399.   *gcc -c hello.c -o hello.o
  400.  
  401. will tell gcc to run the C compiler on the source file c.hello. The resultant
  402. output will be stored in the directory 'o' as hello. Files in 'o' are called
  403. Acorn Object Format (AOF) files.
  404.  
  405. During multistage compilation (i.e. where a program is made up from several
  406. source files), the compiler will compile one file at a time and this will
  407. produce several AOF files.
  408.  
  409. Each source file might contain references to variables and functions that are
  410. not defined within its own scope. For an executable to be created, these
  411. references must be resolved and that is the purpose of the Linker.
  412.  
  413. The Linker takes AOF files and attempts to resolve references between them.
  414. Not all references are defined within AOF files, some (like printf) are
  415. defined within libraries - called Acorn Library Format (ALF) files. So the
  416. Linker will also look at ALF files to match unresolved symbols.
  417.  
  418. For example, below is a list of C source files that will be compiled to make
  419. the program called Harry. Along with each source file is a list of the
  420. symbols that are defined within that source, and a list of symbols that
  421. are not defined (i.e. referenced) by that source.
  422.  
  423.     C source: fred.c
  424.       Symbols defined: foo, bar, pint
  425.       Symbols referenced: printf, puts, food
  426.     
  427.     C source: bob.c
  428.       Symbols defined: fags, beer, food
  429.       Symbols referenced: bar, pint
  430.     
  431.     C source: harry.c
  432.       Symbols defined: main
  433.       Symbols referenced: foo
  434.  
  435. Using gcc to compile fred.c, bob.c, harry.c will produce three AOF files,
  436. fred.o, bob.o and harry.o.
  437.  
  438. harry.c contains a reference to 'foo' which is defined within c.fred. So
  439. the Linker must combine harry.o and fred.c to resolve a reference.
  440.  
  441. fred.c contains a reference to 'printf', 'puts' and 'food'. 'food' is
  442. defined by bob.c so bob.o will also be combined with harry.o and fred.o
  443. to resolve further references.
  444.  
  445. 'printf' and 'puts' are defined by the C library, therefore the linker will
  446. need to combine the ALF file with harry.o, fred.o and bob.o to finally
  447. generate an executable.
  448.  
  449.  
  450. Obtaining compilers
  451. -------------------
  452.  
  453. As said earlier, GCC is a compiler front end. The compilers themselves
  454. can be found at http://gccsdk.riscos.info/
  455.  
  456.  
  457. Distribution patches
  458. --------------------
  459.  
  460. From time to time bugs might be fixed in the GCC distribution. To save
  461. download time involved in uploading/fetching new binaries, patch archives
  462. will become available from the address:
  463.  
  464.   http://gccsdk.riscos.info/
  465.  
  466. Announcements will be posted to comp.sys.acorn.announce whenever new
  467. patch archives are uploaded.
  468.  
  469.  
  470. Contacting the author
  471. ----------------------
  472.  
  473. A mailing list has been setup for discussions on GCC, including development
  474. work.  To join it, send mail to majordomo@gccsdk.riscos.info and put
  475. `subscribe gcc' as the only item of text in the message body.
  476.  
  477.  
  478. Location of the main GCC sources
  479. --------------------------------
  480.  
  481. The official GCC website is http://gcc.gnu.org
  482. The official GNU website is http://www.gnu.org
  483.