home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / gcc / !GCC / !Intro < prev    next >
Encoding:
Text File  |  1994-10-06  |  12.4 KB  |  390 lines

  1.                 GCC on the Archimedes - An introduction
  2.                 =======================================
  3.  
  4. Following a number of justifiable complaints I have received about the GCC
  5. documentation, this is an attempt to explain how to use GCC. Note this is
  6. mainly about the RISC OS specifics of GCC - if you don't know C, C++ or RISC
  7. OS, get youself a good book - you WILL need it.
  8.  
  9. IMPORTANT: GCC REQUIRES A LOT OF MEMORY. The wimpslot needs to be at least
  10. 3044K in size.. GCC is NOT capable of grabbing extra memory, so the initial
  11. WimpSlot sets the maximum memory available.
  12.  
  13. This document is not the same as the version posted on the comp.sys.acorn
  14. newsgroup - there are some significant differences.
  15.  
  16. Index
  17. ~~~~~
  18.  
  19. 1)      Installing GCC
  20. 2)      Filenames
  21. 3)      Simple usage
  22. 4)      More complicated usage
  23. 5)      Using other libraries
  24. 6)    AMU and other bits
  25. 7)      Example programs
  26.  
  27.  
  28.  
  29. 1)      Installing GCC
  30.         ~~~~~~~~~~~~~~
  31.  
  32. Before installing GCC, it should be noted that the standard GCC distribution
  33. is not complete, and only consists of the C and C++ compilers and a few
  34. support programs.
  35.  
  36. In addition to GCC, you will need the following :
  37.  
  38.     *   Linker
  39.         GCC may be used with either the Acorn linker supplied with the
  40.         Norcroft C compiler or with the DRLink linker, which is the default
  41.         linker.
  42.  
  43.     *   Runtine Library
  44.         Again GCC may be used with the Norcroft libraries (Stubs, AnsiLib)
  45.         or with UnixLib, the default library, available from your favorite
  46.         FTP site.
  47.  
  48.  
  49.  
  50.     *   Virtual
  51.         This may not be needed, if you have a lot of RAM (i.e 8M+),
  52.         otherwise it is almost essential.
  53.  
  54. libg++ is included in the distribution.
  55.  
  56. The (default) installation of GCC is relatively simple :
  57.  
  58.     *   Copy the !GCC directory, and its files, onto your hard disc.
  59.     *   If you using the Acorn linker,Shift-double click on the !GCC icon,
  60.         and again on the file '!Boot', to load it into an editor. Find the
  61.         line '|Set GCC$Linker link', and remove the leading '|' character.
  62.         Save the file back to its original place. Repeat with the !Run file
  63.     *   Ensure that the file !Gcc.!boot is run during your boot up
  64.         procedure. This will setup all the paths required by GCC.
  65.  
  66. Note that the first stage will differ slightly, depending on where you
  67. obtained GCC, and the distribution format. Accompanying each distribution
  68. should be a file called !GCCUnpack with more specific instructions.
  69.  
  70. If you wish to separate the distribution (not recommended), the files in
  71. !Gcc.bin should be copied so that they are in your run path, while GCC$Path
  72. and GPP$Path should point to the GCC and GPP directories respectively.
  73.  
  74. When installing UnixLib, follow the instructions supplied with them.
  75.  
  76. libg++ is installed by copying the file 'c++' to the directory '!GCC.gpp.o',
  77. while the 'h' directory is copied to the '!GCC.gpp' directory.
  78.  
  79. If you have an old version of GCC, which you are using with DRLink, the old
  80. installation required 'drlink' to be renamed to 'link'. GCC now defaults to
  81. using DRLink, and the file should be renamed back to DRLink.
  82.  
  83. This completes the installation of GCC.
  84.  
  85.  
  86.  
  87. 2)      Filenames
  88.         ~~~~~~~~~
  89.  
  90. Due to the naming conventions used on UNIX systems, and the use of '.' as
  91. the directory seperator in RISC OS, the filenames used for sources are a
  92. little odd.
  93.  
  94. On UNIX system, the type of a file (C, C++, header file etc) is indicated by
  95. postfixing a file extension. For C and C++ programs, these extensions are :
  96.  
  97.         .c      C sources
  98.         .cc     C++ sources
  99.         .h      header files
  100.         .o      object files (compiled sources)
  101.  
  102. Due to the way that ADFS works, it is not possible to use these extensions
  103. as the '.' causes problems. The work around is to store the files in
  104. subdirectories of the same name ('c', 'cc', 'h' and 'o').
  105.  
  106. Thus the source file 'HelloW.c' would be stored as the file 'HelloW' in the
  107. directory 'c'. See the following table for more examples.
  108.  
  109.         UNIX name       Actual name
  110.         ~~~~~~~~~       ~~~~~~~~~~~
  111.  
  112.         HelloW.c        c.HelloW
  113.         include.h       h.include
  114.         HelloW.cc       cc.HelloW
  115.         object.o        o.object
  116.         asm.s           s.asm
  117.  
  118. Despite this arrangement, all the files are still referred to by their UNIX
  119. names.
  120.  
  121.  
  122.  
  123. 3)      Simple usage
  124.         ~~~~~~~~~~~~
  125.  
  126. For simple usage, programs may be compiled and linked simply by invoking GCC
  127. with the sources listed as part of the command line. For example, the
  128. program 'HelloW.c' can be compiled and linked by typing :
  129.  
  130.         gcc -v HelloW.c
  131.  
  132. This will produce the file !RunImage, which can be run immediately. Reember
  133. you will need 3M+ of memory in the CURRENT wimpslot.
  134.  
  135. If the program is split over several source files, these may be compiled and
  136. linked with a similar command :
  137.  
  138.         gcc -v Example1a.c Example1b.c Example1c.c
  139.  
  140. Which will again produce !RunImage.
  141.  
  142. When compiling C++ programs that use the IOSTREAM library, the switch
  143. '-lc++' should be appended to the command line:
  144.  
  145.     gcc -v HelloW++.cc -lc++
  146.  
  147. The -v switch can be omitted if you dont want the extraneous information.
  148.  
  149. 4)      More complicated usage
  150.         ~~~~~~~~~~~~~~~~~~~~~~
  151.  
  152. GCC's behaviour can be controlled by various switches on the command line.
  153. The following switches are those most likely to be useful.
  154.  
  155.     -S  Compile to assembler code. By default, the output is placed in the
  156.         's' directory.
  157.  
  158.     -c  Compile, but do not link. By default, the object file is placed into
  159.         the 'o' directory.
  160.  
  161.     -mamu
  162.         This is intended for use with 'amu'. It generates the !Depend file
  163.         that AMU uses for its 'dynamic dependencies'. This is now deprecated,
  164.         though I don't yet know what will replace it - my first option
  165.         failed badly.
  166.  
  167.     -mstubs
  168.         This causes GCC to use the norcroft C header files and the shared C
  169.         library stubs. To use this, they must be set up as for norcroft,
  170.         with C$Path pointing to the appropriate directory. This is now
  171.         deprecated - see 'Using other Libraries' for the recommended
  172.         procedure.
  173.  
  174.     -o <file>
  175.         This directs GCC to place the output file in <file>
  176.  
  177.     -O/-O2
  178.         Optimise the generated code. -O2 does more optimsations, but
  179.         requires more memory, and more time.
  180.  
  181. It should be noted that the -mstubs and -mamu switches are likely disappear
  182. in the 2.6.x version. There may be a change over period in which they still
  183. exist.
  184.  
  185. There are some other, more specialised, switches, but these are only likely
  186. to be useful under very rare circumstances, and you should contact me for
  187. more details.
  188.  
  189.     -mno-apcs
  190.         Do not use the APCS-R calling standard.
  191.  
  192.     -mno-function-name
  193.         Do not embed function names in the code.
  194.  
  195.     -mfpa
  196.         Perform some scheduling of floating point instructions.
  197.  
  198.     -mno-stack-check
  199.         Do not perform stack checking.
  200.  
  201.  
  202.  
  203. 5)      Using other libraries
  204.         ~~~~~~~~~~~~~~~~~~~~~
  205.  
  206. By default, GCC uses the UnixLib run-time library, but other libraries can
  207. be used.
  208.  
  209. Libraries can be split into two types :
  210.  
  211. Language support libraries, which supply the basic functions for the
  212. language, such as I/O and maths routines. The currently available libraries
  213. of this type are UnixLib and the Norcroft libraries (Stubs and ANSILib). One
  214. of these libraries MUST be used.
  215.  
  216. 'Application' libraries. These provide extra facilities to the language but
  217. are not part of the language. Examples are RISC_OSLib, DeskLib, Spirit and
  218. OSLib
  219.  
  220. To use an application library, the '-I', '-l', and '-L' switches are used:
  221.  
  222.     -I<dir>
  223.         This tell GCC where to find the header ('.h') files for the library.
  224.         This should be the directory ABOVE the 'h' directory that the header
  225.         files are kept in. I.e -IOSLib: tells GCC to look in the OSLIB:h
  226.         directory for header files.
  227.  
  228.     -l<lib>
  229.         This specifies the name of a library to link against. This may
  230.         either be the complete path to the library (ie. '-lOSLib:o.OSLib')
  231.         or, in conjunction with -L, just the leaf name of the library (ie.
  232.         '-lOSLib').
  233.  
  234.     -L<dir>
  235.         This specifes a directory that GCC should examine while looking for
  236.         libraries. Note, this should be the directory ABOVE the 'o'
  237.         directory that the library resides in. I.e '-LOSLib:' tell GCC to
  238.         look in the 'OSLib:o' directory for library files.
  239.  
  240. Multiple '-I', '-l' and '-L' switches may be used, and GCC will search
  241. through these to find the appropriate header files and libraries. GCC will
  242. search the directories in the specified order.
  243.  
  244. Ie. the switches '-IA: -IB: -LA: -LB: -la -lb' will cuase GCC to look in the
  245. 'A:h' and 'B:h' directories for header files until it finds the specified
  246. file, and it will search for the libraries 'A:o.A' 'B:o.A' 'A:o.B' 'B:o.B'
  247.  
  248. As an example, the OSLib library would, assumeing OSLib$Path points to the
  249. OSLib directory, be used as follows.
  250.  
  251.         gcc <prog> -IOSLib: -lOSLIB:o.OSLib
  252.  
  253. or
  254.  
  255.         gcc <prog> -IOSLIB: -lOSLib -LOSLib:
  256.  
  257. If you wish to replace the language support library (UnixLib), the procedure
  258. is slightly more complicated. If you are using the 'libg++' IOSTEAMS library,
  259. you MUST link against UnixLib, therefore this procedure should only be used
  260. for C programs, not C++.
  261.  
  262. To replace the language library, the following switches are used:
  263.  
  264.   -nostdinc -I<app> -IGCC: -I<lang> -nostdlib -lGCC:o.gcc <lib-specs>
  265.  
  266. the '-I<app>' and '-I<lang>' should be replaced by the appropriate '-I'
  267. switches for the application and language libraries respectively. Note, the
  268. '-I<lang>' MUST go after the '-IGCC:'
  269.  
  270. <lib-specs> should be the appropriate '-l' and '-L' switches. As an example,
  271. when using the shared C library, the following command line is appropriate.
  272.  
  273.         gcc <prog> -nostdinc -IGCC: -IC: -nostdlib -lGCC:o.gcc -lC:o.stubs
  274.  
  275. This assumes that C$Path is set up correctly.
  276.  
  277. Note, that when specifing include paths and libraries, you have to be very
  278. careful with the ordering. For include paths, the order is mentioned above,
  279. while for libraries the ordering is :
  280.  
  281.         Application library
  282.         IOSTREAM library (if used)
  283.         gcc library (gcc:o.gcc)
  284.         language library (Unix:o.Unixlib, etc)
  285.  
  286. If you with to use other libraries with GCC, there are a couple of points
  287. that have to be looked at.
  288.  
  289. For GCC and G++, there are few extra keywords, that cannot be used for
  290. variable names - these are 'new', 'delete', 'this', 'template', 'asm' (and
  291. probably a few others I've messed). You will soon notice if these are used.
  292.  
  293. To provide type safe linkage, C++ 'mangles' function names to include the
  294. types of the parameters. To prevent C++ from mangling C function names, the
  295. dunction is declared as a C function by preceding the declaration with
  296. 'extern "C"', ie.
  297.  
  298. extern "C" fred(int jim);
  299.  
  300. For header files, the standard technique is to bracket the declarations by
  301. the following:
  302.  
  303. #ifdef __cplusplus
  304. extern "C" {
  305. #endif
  306.  
  307. ...
  308.  
  309. #ifdef __cplusplus
  310.         }
  311. #endif
  312.  
  313. It is possible that you library does not have these in the header files, but
  314. these may be easily added.
  315.  
  316.  
  317. The normal format of header files is :
  318.  
  319. #ifndef GUARD
  320. #define GUARD
  321.  
  322. #include <...>
  323.  
  324. <declarations>
  325.  
  326. #endif
  327.  
  328. This should be changed to:
  329.  
  330. #ifndef GUARD
  331. #define GUARD
  332.  
  333. #include <...>
  334.  
  335. #ifdef __cplusplus
  336. extern "C" {
  337. #endif
  338.  
  339. <declarations>
  340.  
  341. #ifdef __cplusplus
  342.         }
  343. #endif
  344.  
  345. #endif
  346.  
  347.  
  348.  
  349.         
  350. 6)    AMU and other bits
  351.     ~~~~~~~~~~~~~~~~~~
  352.  
  353. GCC works quite well with AMU, though there are a few problems - AMU does not
  354. properly handle the '.cc' extension, and in the dependencies lists, you have
  355. to specify the 'cc' as a prefix - see the example Makefile.
  356.  
  357. If you are doing seperate compilation / link stages, there are two ways to do
  358. the linking. You can either let GCC work it out, or use the LD program.
  359.  
  360. To use GCC, all need to type is
  361.  
  362.     gcc -o <output-name> <file1>.o <file2>.o ...
  363.  
  364. possibly with the -lc++ switch if using iostreams.
  365.  
  366. Alternatively, you can directly use the 'ld' command. This is a front end to
  367. link / Drlink which converts the '-L' and '-l' switch to the format required
  368. by link.
  369.  
  370. For an example of using ld, see the example makefile.
  371.  
  372. In addition, there is a program called 'demangle' which converts mangled C++
  373. names to their unmangled versions.
  374.  
  375.  
  376.  
  377. 7)      Example programs
  378.         ~~~~~~~~~~~~~~~~
  379.  
  380.  
  381. There are three example programs supplied with this distribution - two
  382. versions of the classic Hello world program (C and C++), and a three part
  383. program. The command lines for compiling these are
  384.  
  385.     gcc -v -o HelloW HelloW.c
  386.  
  387.     gcc -v -o HelloW++.cc HelloW++.cc -lc++
  388.  
  389.     gcc -v -o Example1 Example1a.c Example1b.c Example1c.c
  390.