Note: This tutorial was written for me by James Bursa after a request on the RISC OS Projects Initiative page. You can contact James at james.bursa@strcprstskrzkrk.co.uk.
As GCC is under constant development, this tutorial may change on a regular basis, so please check back.
This document gives an overview of using the GNU Compiler Collection (GCC) for RISC OS. It assumes some knowledge of the RISC OS environment and programming, but none of compilers. However, it does not contain a C tutorial: get a textbook.
GCC is a command-line tool, unlike most RISC OS programs which have a graphical interface. To use it, you have to type commands into a Taskwindow.
You will need at least 25MB of hard disc space.
Get the compiler zip archives from http://hard-mofo.dsvr.net/gcc/. You should get all the
archives, except g77.zip
(unless you want to use Fortran) and java.zip
(unless you want to compile Java). You only need cc1plus.zip
if you want to use
C++. Each archive contains a !gcc application and they should all be extracted into the
same place to merge the contents. If you have a previous version of gcc, do not merge
the new version with it, but move it somewhere else.
You may wish to install an editor. Edit is sufficient, but Zap or StrongEd provide additional features (for example code colouring).
To check the compiler has been installed correctly, follow these steps:
gcc -v
(enter at end) at the *
prompt. The
output should be something likeUsing builtin specs.
gcc version 2.95.4 20010319 (prerelease) [gccsdk 20010912]
This section walks you through compiling a simple C program using gcc. The text file containing the program is known as a source file.
To get to the example programs, shift-double-click on !gcc, and open the examples directory.
Make sure that there's plenty of memory free, and the next slot is at least 5MB, and start a Taskwindow.
RISC OS has the concept of a 'current directory'. This is a directory which is looked in
when a program wants a file, and gcc looks there for source files. The first thing to do is to
change the current directory to the examples directory containing the source file using the
Dir
command, so type
Dir ADFS::HardDisc.$.Tools.!gcc.examples
giving the path to the examples directory on your machine (try typing Dir
, and
dragging the directory into the taskwindow while holding shift to enter the path
automatically).
Type Ex
to check that you have changed to the correct directory. This command
gives a list of the files in the current directory. You should see 'c' in the list.
Now we're ready to compile. Type this command:
gcc hellow.c
In a few moments, a file called '!RunImage' will appear in the examples directory. This is the compiled program, and you can double-click on it to run it.
GCC comes from unix, where extensions are used to identify file types. For example, the
extension .c
indicates C source. RISC OS can't use extensions, so instead files
are kept in a directory named after the extension. C source files are kept in a directory
called 'c', and header files are kept in a directory called 'h'.
The gcc command expects to get file names as if they had extensions. It then does the
conversion of the extension to a directory name itself. For example, to compile a C source file
called 'test' (located in a directory 'c') , you need the command gcc test.c
.
C source files can be edited in any text editor. On RISC OS, you could use Edit from the application suite. You can also download StrongEd or Zap or use a different editor.
To control gcc, options are given after the gcc command. Options start with a
-
, are separated by spaces, and order does not matter. For example, the
-v
(verbose) option makes gcc give more detail of what it's doing:
gcc -v hellow.c
This is equivalent to
gcc hellow.c -v
Anything that doesn't start with -
is the name of a file to compile.
Here's a more complicated example:
gcc -v -W -O test1.c test2.c
This compiles test1 and test2 with verbose output (-v
), warnings on
(-W
), and optimisation enabled (-O
).
The compiler is actually a group of programs, which work together to create the final executable. The programs include:
#include
directives and macros.The compilation process
Libraries are collections of functions written by someone else, which you can use in your
programs. For example, the standard C library contains useful functions needed by most C
programs, such as printf
. A library consists of a library file which
contains the actual functions already compiled, and some header files (or
headers) which describe what functions the library file contain, as C declarations.
To use a library, the headers need to be included in the C source using
#include
statements. The preprocessor will then insert the contents of the header
at this point.
GCC looks for included files in the following places:
-I
.For example, the command
gcc -IADFS::HardDisc.$.Code.MyLibrary example.c
with the line
#include "test.h"
in c.example, would make gcc look for h.test in the directory containing c, in the standard directory, and in ADFS::HardDisc.$.Code.Extra.
Also, the library needs to be made available to the linker, so that it can include the compiled functions in the program. This is done by giving the pathname of the library file to gcc:
gcc -IADFS::HardDisc.$.Code.MyLibrary ADFS::HardDisc.$.Code.MyLibrary.mylibrary.o example.c
(note that this actually refers to the library file
ADFS::HardDisc.$.Code.MyLibrary.o.mylibrary
-- gcc converts the last extension to
a directory).
The standard C library is automatically used, so doesn't need to be specified like this.
Here is a description of the most useful options to gcc. For a full list of options, read the gcc manual (in !gcc.docs).
-o name
-v
-Wall
-c
-O
-O2
, -O3
-Idirectory
-llibrary
-mthrowback
GCC can use two different standard C libraries:
-mlibscl
to gcc.The Shared C Library is a RISC OS module which can be used by more than one C program simultaneously, saving memory. The compiler will add special code to your program which transparently passes calls to library functions to the module.
There are some differences between the headers for the Shared C Library and UnixLib, so
do not mix object files compiled with -mlibscl
with those compiled without.
If you don't know which to use, use the Shared C library.
C++ source files are kept in a 'cc' directory (alternatively, 'cpp' or 'c++' also works). Compilation is similar to C.
It is not possible to use the Shared C library with C++; use UnixLib.
Additional command line options need to be specified for some C++ libraries:
-lstdc++
to link with the standard C++ library. This is required for use
of the stl, for example the headers <string>
and <vector>
.-liostream
to link with the iostream library. Use this for
<iostream>
.For example, to compile the helloworld program in examples.cc, which uses the iostream classes:
gcc -liostream helloworld.cc