home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d03xx / d0340.lha / NorthC / NorthC.lzh / hello.doc < prev    next >
Text File  |  1990-02-27  |  10KB  |  290 lines

  1.   (c) 1990 S.Hawtin.
  2.   Permission is granted to copy this file provided that:
  3.    1) It is not used for commercial gain
  4.    2) This notice is included in all copies
  5.    3) Altered copies are marked as such.
  6.  
  7.   No liability is accepted for the contents of the file.
  8.  
  9.  
  10.  
  11.   Welcome to NorthC, this is a public domain 'C' programming system,
  12. put together out of parts from people too numerous to mention.  This
  13. disk contains all you require to start creating or porting 'C' programs
  14. on your Amiga.
  15.  
  16.   So, you have just got this new disk from the public domain, and you are 
  17. wondering how to become a top 'C' wizard in an afternoon.  Well you must
  18. start by writing a program.  Lisp programmers always write factorial as
  19. thier first program, but that is because they are strange.  'C' being a
  20. "real" language all 'C' programmers start with the "Hello World" program.
  21.  
  22.   This file outline the steps you should take to create a simple 'C'
  23. program, compile it and run it.  This should work on anything from an
  24. unexpanded Amiga 500 upwards, however if you have an unexpanded Amiga 500
  25. go out and buy a second drive, they don't cost much and they make your 
  26. life so much easier.
  27.  
  28.  
  29.   In this set of instructions I am assuming that the floppy you are using
  30. is called "NorthC", if it is called something else, for example "Lib298",
  31. then you will have to type the name of the floppy every time I say you
  32. should type "NorthC:"
  33.  
  34.  
  35.   The steps you should take are as follows.
  36.  
  37.  
  38. 1) Backup Your Disk
  39.  
  40.   Write protect the original NorthC disk, so you can see through the hole,
  41. then either use the "Workbench"-"Duplicate" option, or the command "diskcopy"
  42. to make a copy of the NorthC floppy.  Once you have done this always use
  43. the copy, that way if something goes wrong you can make another copy.
  44.  
  45.  
  46. 2) Start the CLI
  47.  
  48.   The 'C' environment requires that you are running in a shell.  Double 
  49. click the shell icon in the "Workbench 1.3" floppy.
  50.  
  51.  
  52. 3) Set up the NorthC environment
  53.  
  54.   You will need to do this every time you run the 'C' compiler, once you
  55. get bored enough you can put this in the startup-sequence file.
  56.  
  57.   How you set up the environemnt depends on your configuration, there are
  58. three posibilities, either you have a hard disk, you have a second floppy,
  59. or you only have a single floppy.
  60.  
  61.  
  62. 3a) A Single Floppy
  63.  
  64.   Type the command
  65.  
  66.     execute NorthC:Single-Disk
  67.  
  68. this will copy the necessary programs into the ram disk, once the programs 
  69. are copied you can "delete ram:copy" to give yourself more room for
  70. compiling programs.
  71.  
  72.  
  73. 3b) If you have a second floppy drive
  74.  
  75.   Place the NorthC floppy into the second floppy drive, and type
  76.  
  77.     cd NorthC:
  78.     setup-NorthC
  79.  
  80. this will add the "bin" directory to the search path and tell the system
  81. where to find the 'C' libraries.
  82.  
  83.  
  84. 3c) If you have a hard drive
  85.  
  86.   Copy the files in the "NorthC:bin" directory into your executables
  87. directory, copy the include files from "NorthC:include" into "include",
  88. copy the files in "NorthC:libs" into a suitable 'C' library directory, 
  89. for example "clibs" and add the line
  90.  
  91.     assign clibs: hd0:clibs
  92.  
  93. into your "s:startup-sequence" file.
  94.  
  95.  
  96. 4) Move to the source directory
  97.  
  98.   The command
  99.  
  100.     cd NorthC:examples/hello
  101.  
  102. should move you to a suitable empty source directory, this is where you will
  103. create the "Hello World" program.
  104.  
  105.  
  106. 5) Create a source file
  107.  
  108.   You should use "memacs" from the 1.3 Extras disk to enter a 'C' program,
  109. the command
  110.  
  111.     "Extras 1.3:tools/memacs" hello.c
  112.  
  113. will start the editor up, enter the following file
  114.  
  115.     #include <stdio.h>
  116.  
  117.     main()
  118.        {/* The simplest 'C' program ever */
  119.     printf("\a\aHello World\n");
  120.     }
  121.  
  122. once you have typed this in select "save-exit" on the "Project" menu, this
  123. will save the file to the disk and exit the program.
  124.  
  125.   You should learn to use "memacs" when typing 'C' programs, it is a 
  126. version of the most widely used program editor for 'C' programmers.  It has
  127. a large number of features that make it very powerfull for editing programs.
  128.  
  129.  
  130. 6) Compile the file
  131.  
  132.   If you called your file "hello.c" type the command 
  133.  
  134.     cc hello.c
  135.  
  136. after much clunking of disk drives, and disk swapping if you have just 
  137. the one drive, you will find that your directory contains a file "a.out" 
  138. as well as "hello.c".  Use the command
  139.  
  140.     list
  141.  
  142. to show you the contents of the directory.
  143.  
  144.  
  145. 7) Run the file
  146.  
  147.   Once "a.out" has been created you can run the program by typing
  148.  
  149.     a.out
  150.  
  151. you will be amazed by the result, or there again you might not be.
  152.  
  153.  
  154. 8) Go again
  155.  
  156.   Edit the "hello.c" file to do something else, get ideas from published
  157. 'C' programs, public domain programs, friends, and anywhere else you can.
  158.  
  159.   There are a number of example programs on this disk look in the "examples"
  160. directory and the "tools" directory, steal all the good ideas from them (if 
  161. you can find any).
  162.  
  163.   You should normally compile your programs with a command such as
  164.  
  165.     cc -ohello hello.c
  166.  
  167. this will create a program called "hello" rather than "a.out", this makes it
  168. easier to remember what the program does.
  169.  
  170.  
  171. 9) Get serious
  172.  
  173.   Buy yourself a copy of "Kernigan & Ritchie" or better yet get someone else
  174. to buy one for you.  I know it is expensive but if you get seriously into
  175. 'C' you will get your own copy in the end anyway.  It really is the best
  176. book on 'C' ever written, even if the code layout is a bit weird.
  177.  
  178.   Find a good book for AmigaDOS, I cannot recommend any book here as I 
  179. have yet to find one that doesn't require a degree in computer science
  180. to understand, however I am sure that some easy to understand books are
  181. around.
  182.  
  183.   Read the "libc.doc" file on this disk to see what is currently implemented
  184. in the NorthC library, read the "Bugs.doc" file especially if a program
  185. does something you really didn't expect, or if it fails to compile.
  186.  
  187.  
  188. 10) Multiple files
  189.  
  190.   As a taster for the complexities of programming I will explain about
  191. compiling large programs that are split over many files, ignore this bit
  192. if you want.
  193.  
  194.   Once you have a decent sized program you will want to split it over many
  195. source files, suppose you have a program that is written in the files
  196. "foo.c" "bar.c" and "baz.asm".  You can compile the files with the command
  197.  
  198.     cc -ofoo foo.c bar.c baz.asm
  199.  
  200. however this will recompile all three files each time you compile the 
  201. program.
  202.  
  203.   The 'C' world contains four different types of file, source files,
  204. assembler files, object files and executable files.  You can usually tell
  205. which category any file comes in by its extension, for example source file 
  206. names end in ".c", for example "foo.c" "this.is.source.c" and so on.  Object
  207. files normally end in ".o", assembler files end in ".s" or ".asm", and
  208. executable files have no extensions.
  209.  
  210.   The 'C' compilation process contains three stages, first a compiler that 
  211. translates source files into assembler files, then an assembler that translates 
  212. assembler files into object files, and finally a linker that combines a number
  213. of object files to create an executable.  The "cc" command will call each of
  214. these stages for you, when you type 
  215.  
  216.     cc -ohello hello.c
  217.  
  218. cc will first call the compiler, called NorthC, to create a file "hello.s", 
  219. next cc will call the assembler, called A68K, to create a file "hello.o", 
  220. and finally it will call the linker, Blink, to make the executable "hello", 
  221. once this is created it will delete the intermediate files "hello.s" and 
  222. "hello.o".
  223.  
  224.   We can tell cc not to bother with the linking stage by passing the "-c"
  225. flag, for example the command 
  226.  
  227.     cc -c hello.c
  228.  
  229. will create the "hello.o" file then stop.  Similarly 
  230.  
  231.     cc -c bar.c
  232.     cc -c baz.asm
  233.  
  234. will create the files "bar.o" and "baz.o", in the case of "baz.asm" cc 
  235. knows it is an assembler file because its name end in ".asm" so it just 
  236. calls A68K to assemble it.
  237.  
  238.   Once we have "foo.o" "bar.o" and "baz.o" we can link them together with
  239. the command
  240.  
  241.     cc -ohello foo.o bar.o baz.o
  242.  
  243. this will complete the compilation process, calling Blink to produce the 
  244. executable file "hello".  
  245.  
  246.   If we now find that there is a mistake in our "foo.c" source file we just 
  247. change "foo.c" the we only have to compile this one file, with 
  248.  
  249.     cc -c foo.c
  250.  
  251. and then link all the files, with 
  252.  
  253.     cc -ohello foo.o bar.o baz.o
  254.  
  255. rather than having to compile all the files again.
  256.  
  257.   Once you have understood all that you should find a good 'C' book and look
  258. up libraries, not to be confused with Amiga libraries, this will get you
  259. nicely confused again.  I hope that didn't confuse you.
  260.  
  261.   Examine the source code for "cc" in tools, this will show what "cc" can do,
  262. if you would rather that it did something differently change the file "cc.c"
  263. make a note of the fact you have changed it at the top of the file and 
  264. recompile it.  "cc.c" was hacked together in a single evening with just the 
  265. UNIX "cc" manual for a spec, so I won't mind if you 'improve' it, provided
  266. you note what changes you have made at the top of the source.
  267.  
  268.  
  269. 11) Share and Enjoy (or Go Stick Your Head in a Pig)
  270.  
  271.   Once you understand 'C' you should share your knowledge, if you have an
  272. interesting program put it into the public domain or make it shareware, if
  273. you find learners in 'C' and AmigaDOS explain how to do things, always 
  274. remember that you once knew nothing about computers, even if it was a long 
  275. time ago.  Most of all you should have fun with computers, unless of course 
  276. you are a COBOL programmer.
  277.  
  278.  
  279. 12) After
  280.  
  281.   If you are a real 'C' wizard go off and learn a proper language, get a
  282. copy of "scheme", on Fish 149, and "The Structure and Interpretation of 
  283. Computer Programs" by Ableson & Sussman.  The factorial function is
  284.  
  285.     (define (factorial n)
  286.         (if (= n 0) 1
  287.             (* n (factorial (- n 1)))))
  288.  
  289. there I told you Lisp programmers were weird.
  290.