home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
dev
/
cmanual-3.0.lha
/
CManual
/
Amiga
/
Introduction
/
Introduction.doc
< prev
next >
Wrap
Text File
|
1993-10-12
|
35KB
|
1,125 lines
1 INTRODUCTION
1.1 INTRODUCTION
In this chapter we will look at how C compilers work, how to
use the Lattice C compiler, repeat some important things in C,
and finish of by explaining how two use the Amigas's Libraries,
and discuss the three different types of memory. As you have
noticed, this chapter will give you a short introduction to C,
compilers and the Amiga.
1.2 HOW TO COMPILE AND LINK
The idea with C compilers is that you write a document
(usually referred as "source code"), which is read by the
C compiler in two stages. The first time the C compiler will
create a working file, referred as "quad file". The quad file
is then compiled a second time into an "object file".
The object file contains mostly executable machine code, but it
also contains references to external functions/variables in
other object files and libraries. So before we can run the
program we need to tie all object files and libraries together
into one single program. It is done by the linker.
-------------
| Program.c | Source Code
-------------
|
| 1. First phase of compilation (lc1)
V
-------------
| Program.q | Quad file
-------------
|
| 2. Second phase of compilation (lc2)
V
-------------
| Program.o | Object file
-------------
|
| 3. Linking (blink)
V
-------------
| Program | Executable program
-------------
1.2.1 SOURCE CODE
When you are going to write C programs you need an editor/
wordprocessor to create the "source code". The source code is
like a normal document except that it is written following some
special rules. Almost any kind of editor/wordprocessor can be
used. You can even use the MEMACS which you got when you bought
your Amiga. (MEMACS exist on "The Extras Disk".)
Here is an example of a source code:
#include <stdio.h>
main()
{
printf("Hello!\n");
}
Once you have written the source code you need to save it. It
is standard that the file names should end with the extension
".c". So if you want to call your program "hello", the source
code should be named "hello.c".
1.2.2 FIRST PHASE OF COMPILATION
Once the source code is saved you can start the first phase of
compilation. If you use the Lattice C Compiler you simply use
the command "lc1". To compile our example you only need to
write "lc1 hello.c", and the compiler will then create a quad
file named "hello.q".
1.2.3 SECOND PHASE OF COMPILATION
To start the second phase of compilation you use the command
"lc2". To compile our example you then only need to write
"lc2 hello.q", and the compiler will then create an object file
named "hello.o".
If you want to invoke both the first and second compiler pass
in one single call you can use the command "lc". So if you
write "lc hello.c" both the first and the second phase of
compilation would be executed. (lc calls both lc1 and lc2
automatically.)
1.2.4 LINKING
The quad file does not, as said before, contain only machine
code instructions. It also contains calls to external functions
in other object files and libraries. Before you may run the
program you therefore need to tie everything together by a
linker. Lattice C uses a linker called "blink".
Every program you create must normally be linked together with
the startup routine "c.o", and the two libraries: "lc.lib" and
"amiga.lib".
The object file and the two libraries are placed in the "lib"
directory on the second Lattice C disk. That directory is
normally already assigned the name "LIB:", so if you want to
find the files, you simply write "LIB:c.o" for example.
For almost all programs you call the linker like this:
(This shows how to link our example "hello".)
blink LIB:c.o, hello.o TO hello LIB LIB:lc.lib, LIB:amiga.lib
[A] [B] [C] [D] [E] [F]
[A] The object file "c.o" must always be placed before all
other object files. The file can be found in the logical
device "LIB:".
[B] After the startup file "c.o" you may place all other
object files you want to link together. (Remember to put
a comma between them.) In this example we only have one
object file, "hello.o".
[C] After the list of object files you write the word "TO" plus
a name which will be the name of the executable program. In
this example we want our program to be called "hello".
[D] Almost all programs need to be linked together with some
libraries. The keyword "LIB" tells blink that the coming
file-names are names on libraries. (Remember to put a comma
between each library name.)
Do not mix up the keyword "LIB" with the logical device
"LIB:". "LIB" tells the linker that a list of library-names
will come, while the logical device "LIB:" tells AmigaDOS
which device and directory to find some files in.
[E] The library "lc.lib" must be the second last library in the
list. The library can be found in the logical device
"LIB:".)
[F] The library "amiga.lib" must be the last library in the
list. The library can be found in the logical device
"LIB:".)
If you want to invoke both the first and second compiler pass,
plus link the object file together with the standard libraries,
in one single call you can use the command "lc" with the added
parameter "-L". So if you want to compile and link the program
in one single command you only need to write:
lc -L hello.c
The command "lc" will invoke both the first and the second
compiler pass, and the added parameter "-L" means that the
finished object code should be linked together with the
standard libraries.
If you also want the linker to search the standard math library
you simply put the letter "m" after the "-L":
lc -Lm <file-name.c>
Our simple example "hello" does not need the math library, but
if you use any sort of calculations you normally need to
include it. ALL examples in this manual will compile and link
happily with the command:
lc -Lm ExampleX.c
1.3 C PROGRAMS
This manual is written for everyone who has a little knowledge
of C and who wants to program the Amiga. If you know how to
write a program that prints out "hello!" on the screen, and
are not totally confused by pointers and structures, I do not
think you will have any problems reading this manual. All
examples have been written as clearly as possible, and I have
avoided many shortcuts in order to make the examples easily
understandable.
However, before we start with the real manual I think it is
best to repeat some important features in C:
1.3.1 #INCLUDE
The first thing you find in a C program is the "#include"
commands. They tell the compiler to read some files before
compiling the code, and can therefore contain definitions of
constants/structures/macros etc. The files are normally
referred as "header files", and have because of that a ".h"
extension.
There exist two types of header files; your own and standard
system definitions. If you include your own header files you
put a double-quotation around the file name:
#include "mydefinitions.h"
The compiler will then look in the "current directory" for your
file. If you on the other hand include any standard system
definitions you put two angle-brackets around the file name:
(The compiler will then look in the logical device "INCLUDE:")
#include <intuition/intuition.h>
Note that there is not semicolons after the #include
statements! That is because the #include files are scanned
by a macro pre-processor (included in the first compiler phase,
lc1), and have actually nothing to do with the C compiler.
Later in the manual we will often include the file intuition.h
which contains definitions of many structures (such as Window,
Border, Gadget etc) plus some important constants and macros.
This and all other standard system header files are compressed
and put on the second Lattice C disk. If you are interested to
see what they actually define you can find the uncompressed
files on the fourth Lattice C disk.
1.3.2 #DEFINE
After the #include statements the #define statements will come.
With help of the #define command you can both declare constants
and replacement strings, but you can even declare your own
macro