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 >
Text File  |  1993-10-12  |  35KB  |  1,125 lines

  1. 1    INTRODUCTION
  2.  
  3. 1.1  INTRODUCTION
  4.  
  5. In this chapter we will look at how C compilers work, how to
  6. use the Lattice C compiler, repeat some important things in C,
  7. and finish of by explaining how two use the Amigas's Libraries,
  8. and discuss the three different types of memory. As you have
  9. noticed, this chapter will give you a short introduction to C,
  10. compilers and the Amiga.
  11.  
  12.  
  13.  
  14. 1.2  HOW TO COMPILE AND LINK
  15.  
  16. The idea with C compilers is that you write a document
  17. (usually referred as "source code"), which is read by the
  18. C compiler in two stages. The first time the C compiler will
  19. create a working file, referred as "quad file". The quad file
  20. is then compiled a second time into an "object file".
  21.  
  22. The object file contains mostly executable machine code, but it
  23. also contains references to external functions/variables in
  24. other object files and libraries. So before we can run the
  25. program we need to tie all object files and libraries together
  26. into one single program. It is done by the linker.
  27.  
  28.  
  29. -------------
  30. | Program.c |   Source Code
  31. -------------
  32.       |
  33.       |         1. First phase of compilation (lc1) 
  34.       V
  35. -------------
  36. | Program.q |   Quad file
  37. -------------
  38.       |
  39.       |         2. Second phase of compilation (lc2)
  40.       V
  41. -------------
  42. | Program.o |   Object file
  43. -------------
  44.       |
  45.       |         3. Linking (blink)
  46.       V
  47. -------------
  48. |  Program  |   Executable program
  49. -------------
  50.  
  51.  
  52.  
  53. 1.2.1  SOURCE CODE
  54.  
  55. When you are going to write C programs you need an editor/
  56. wordprocessor to create the "source code". The source code is
  57. like a normal document except that it is written following some
  58. special rules. Almost any kind of editor/wordprocessor can be
  59. used. You can even use the MEMACS which you got when you bought
  60. your Amiga. (MEMACS exist on "The Extras Disk".)
  61.  
  62. Here is an example of a source code:
  63.  
  64. #include <stdio.h>
  65.  
  66. main()
  67. {
  68.   printf("Hello!\n");
  69. }
  70.  
  71. Once you have written the source code you need to save it. It
  72. is standard that the file names should end with the extension
  73. ".c". So if you want to call your program "hello", the source
  74. code should be named "hello.c".
  75.  
  76.  
  77.  
  78. 1.2.2  FIRST PHASE OF COMPILATION
  79.  
  80. Once the source code is saved you can start the first phase of
  81. compilation. If you use the Lattice C Compiler you simply use
  82. the command "lc1". To compile our example you only need to
  83. write "lc1 hello.c", and the compiler will then create a quad
  84. file named "hello.q".
  85.  
  86.  
  87.  
  88. 1.2.3  SECOND PHASE OF COMPILATION
  89.  
  90. To start the second phase of compilation you use the command
  91. "lc2". To compile our example you then only need to write
  92. "lc2 hello.q", and the compiler will then create an object file
  93. named "hello.o".
  94.  
  95. If you want to invoke both the first and second compiler pass
  96. in one single call you can use the command "lc". So if you
  97. write "lc hello.c" both the first and the second phase of
  98. compilation would be executed. (lc calls both lc1 and lc2
  99. automatically.)
  100.  
  101.  
  102.  
  103. 1.2.4  LINKING
  104.  
  105. The quad file does not, as said before, contain only machine
  106. code instructions. It also contains calls to external functions
  107. in other object files and libraries. Before you may run the
  108. program you therefore need to tie everything together by a
  109. linker. Lattice C uses a linker called "blink".
  110.  
  111. Every program you create must normally be linked together with
  112. the startup routine "c.o", and the two libraries: "lc.lib" and
  113. "amiga.lib".
  114.  
  115. The object file and the two libraries are placed in the "lib"
  116. directory on the second Lattice C disk. That directory is
  117. normally already assigned the name "LIB:", so if you want to
  118. find the files, you simply write "LIB:c.o" for example.
  119.  
  120. For almost all programs you call the linker like this:
  121. (This shows how to link our example "hello".)
  122.  
  123. blink LIB:c.o, hello.o  TO hello  LIB LIB:lc.lib, LIB:amiga.lib
  124.         [A]      [B]       [C]    [D]     [E]          [F]
  125.  
  126.  
  127. [A] The object file "c.o" must always be placed before all
  128.     other object files. The file can be found in the logical
  129.     device "LIB:".
  130.  
  131. [B] After the startup file "c.o" you may place all other
  132.     object files you want to link together. (Remember to put
  133.     a comma between them.) In this example we only have one
  134.     object file, "hello.o".
  135.  
  136. [C] After the list of object files you write the word "TO" plus
  137.     a name which will be the name of the executable program. In
  138.     this example we want our program to be called "hello".
  139.  
  140. [D] Almost all programs need to be linked together with some
  141.     libraries. The keyword "LIB" tells blink that the coming
  142.     file-names are names on libraries. (Remember to put a comma
  143.     between each library name.)
  144.     
  145.     Do not mix up the keyword "LIB" with the logical device
  146.     "LIB:". "LIB" tells the linker that a list of library-names
  147.     will come, while the logical device "LIB:" tells AmigaDOS
  148.     which device and directory to find some files in.
  149.  
  150. [E] The library "lc.lib" must be the second last library in the
  151.     list. The library can be found in the logical device
  152.     "LIB:".)
  153.  
  154. [F] The library "amiga.lib" must be the last library in the
  155.     list. The library can be found in the logical device
  156.     "LIB:".)
  157.  
  158.  
  159. If you want to invoke both the first and second compiler pass,
  160. plus link the object file together with the standard libraries,
  161. in one single call you can use the command "lc" with the added
  162. parameter "-L". So if you want to compile and link the program
  163. in one single command you only need to write:
  164.  
  165. lc -L hello.c
  166.  
  167. The command "lc" will invoke both the first and the second
  168. compiler pass, and the added parameter "-L" means that the
  169. finished object code should be linked together with the
  170. standard libraries.
  171.  
  172. If you also want the linker to search the standard math library
  173. you simply put the letter "m" after the "-L":
  174.  
  175. lc -Lm <file-name.c>
  176.  
  177. Our simple example "hello" does not need the math library, but
  178. if you use any sort of calculations you normally need to
  179. include it. ALL examples in this manual will compile and link
  180. happily with the command:
  181.  
  182. lc -Lm ExampleX.c
  183.  
  184.  
  185.  
  186. 1.3  C PROGRAMS
  187.  
  188. This manual is written for everyone who has a little knowledge
  189. of C and who wants to program the Amiga. If you know how to
  190. write a program that prints out "hello!" on the screen, and
  191. are not totally confused by pointers and structures, I do not
  192. think you will have any problems reading this manual. All
  193. examples have been written as clearly as possible, and I have
  194. avoided many shortcuts in order to make the examples easily
  195. understandable.
  196.  
  197. However, before we start with the real manual I think it is
  198. best to repeat some important features in C:
  199.  
  200.  
  201.  
  202. 1.3.1  #INCLUDE
  203.  
  204. The first thing you find in a C program is the "#include"
  205. commands. They tell the compiler to read some files before
  206. compiling the code, and can therefore contain definitions of
  207. constants/structures/macros etc. The files are normally
  208. referred as "header files", and have because of that a ".h"
  209. extension.
  210.  
  211. There exist two types of header files; your own and standard
  212. system definitions. If you include your own header files you
  213. put a double-quotation around the file name:
  214.  
  215. #include "mydefinitions.h"
  216.  
  217. The compiler will then look in the "current directory" for your
  218. file. If you on the other hand include any standard system
  219. definitions you put two angle-brackets around the file name:
  220. (The compiler will then look in the logical device "INCLUDE:")
  221.  
  222. #include <intuition/intuition.h>
  223.  
  224.  
  225. Note that there is not semicolons after the #include
  226. statements! That is because the #include files are scanned
  227. by a macro pre-processor (included in the first compiler phase,
  228. lc1), and have actually nothing to do with the C compiler.
  229.  
  230.  
  231. Later in the manual we will often include the file intuition.h
  232. which contains definitions of many structures (such as Window,
  233. Border, Gadget etc) plus some important constants and macros.
  234. This and all other standard system header files are compressed
  235. and put on the second Lattice C disk. If you are interested to
  236. see what they actually define you can find the uncompressed
  237. files on the fourth Lattice C disk.
  238.  
  239.  
  240.  
  241. 1.3.2  #DEFINE
  242.  
  243. After the #include statements the #define statements will come.
  244. With help of the #define command you can both declare constants
  245. and replacement strings, but you can even declare your own
  246. macro