home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example2.LZH / hello / hello.doc < prev    next >
Text File  |  1990-09-08  |  12KB  |  325 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.   This file outlines the steps you should take to create a simple 'C' 
  10. program, compile it and run it.  This should work on anything from an 
  11. unexpanded Amiga 500 upwards, however if you have an unexpanded Amiga 500 
  12. go out and buy a second drive, they don't cost much and they make your 
  13. life so much easier.
  14.  
  15.   So, you have just got this new disk from the public domain, and you are 
  16. wondering how to become a top 'C' wizard in an afternoon.  Well you must 
  17. start by writing a program.  Lisp programmers always write factorial as 
  18. their first program, but that is because they are strange.  'C' being a 
  19. "real" language all 'C' programmers start with the "Hello World" program.
  20.  
  21.   This set of instructions assumes that the NorthC system is on a floppy 
  22. called "NorthC:", if you are running on a hard disk, or a floppy called 
  23. something else, you must make sure that the "NorthC:" device is set up by 
  24. the script file "setup-NorthC".
  25.  
  26.   The steps you should take to produce your first program are as follows.
  27.  
  28. 1) Backup Your Disk
  29.  
  30.   Once you have unpacked your NorthC disks you should make copies of each 
  31. of them.  None of the programs should need to write to the "NorthC:" disk 
  32. so you should write protect that, move the little tab so that you can see 
  33. through the hole.  Either use the "Workbench"-"Duplicate" option, or 
  34. better the CLI command "diskcopy" to make a copy of the NorthC floppy.  
  35. Once you have done this always use the copy, that way if something goes 
  36. wrong you still have the original.
  37.  
  38. 2) Start the CLI
  39.  
  40.   As it currently stands you must run the 'C' compiler from the CLI, you 
  41. must start up a CLI shell.
  42.  
  43. 3) Set up the NorthC environment
  44.  
  45.   You will need to do this every time you run the 'C' compiler, once you 
  46. get bored enough you could put this in the "s:startup-sequence" file.
  47.  
  48.   How you set up the environment depends on your configuration, there are 
  49. three possibilities, either you have a hard disk, you have a second 
  50. floppy,or you only have a single floppy.
  51.  
  52.   Edit the file "Setup-NorthC" on the "NorthC:" disk, run "memacs" from 
  53. the "Extras 1.3" disk, select "Project"-"Visit File" then type 
  54. "NorthC:Setup-NorthC".  At the end of the file are three "execute" 
  55. commands, comment out the two that do not apply, so if you are running on 
  56. a single drive Amiga the end of the file should be
  57.  
  58.     execute NorthC:Single-make
  59.     ;execute NorthC:Dual-disk
  60.     ;execute NorthC:Hard-disk
  61.  
  62. with a ';' in front of the two lines that do not apply.  Then save the 
  63. "setup-NorthC" file back on the "NorthC" disk.
  64.  
  65. 3a) A Single Floppy
  66.  
  67.   Type the CLI command
  68.  
  69.     execute NorthC:Setup-NorthC
  70.  
  71. this will copy the necessary programs into the ram disk.  Once you have 
  72. compiled your first program I would suggest you use a slightly more 
  73. complex way to run the system, see 8a below.
  74.  
  75. 3b) If you have a second floppy drive
  76.  
  77.   Place the NorthC floppy into the second floppy drive, that is "df1:", 
  78. and type
  79.  
  80.     cd NorthC:
  81.     execute setup-NorthC
  82.  
  83. this will add the "bin" directory to the search path and tell the system 
  84. where to find the 'C' libraries.
  85.  
  86. 3c) If you have a hard drive
  87.  
  88.   To run NorthC on a hard drive you need to copy the executables, the 
  89. include files and the libraries on to your hard drive.  Place the 
  90. "NorthC:" disk into the floppy and type
  91.  
  92.     cd NorthC:
  93.     execute setup-NorthC
  94.  
  95. this will copy the executables to the "c:" directory, and create two new 
  96. directories on "hd0:", if these directories are not suitable for your 
  97. setup edit the "NorthC:Hard-Disk" file before running "Setup-NorthC".
  98.  
  99.   Once the files have been copied comment out all the "Hard-Disk" file 
  100. except the final line.
  101.  
  102. 4) Change Directory to the source directory
  103.  
  104.   The command
  105.  
  106.     cd "NorthC Examples:hello"
  107.  
  108. should move you to a suitable directory, this is where you will create the 
  109. "Hello World" program.
  110.  
  111. 5) Create a source file
  112.  
  113.   You should use "memacs" from the 1.3 Extras disk to enter a 'C' 
  114. program,the command
  115.  
  116.     "Extras 1.3:tools/memacs" hello.c
  117.  
  118. will start the editor up, enter the following file
  119.  
  120.     #include <stdio.h>
  121.  
  122.     main()
  123.        {/* The simplest 'C' program ever */
  124.         printf("\a\aHello World\n");
  125.         }
  126.  
  127. once you have typed this in select "save-exit" on the "Project" menu, this 
  128. will save the file to the disk and exit the program.
  129.  
  130.   You should learn to use "memacs" when typing 'C' programs, it is a 
  131. version of the most widely used program editor for 'C' programmers.  It 
  132. has a large number of features that make it very powerful for editing programs.
  133.  
  134. 6) Compile the file
  135.  
  136.   If you are running with a single floppy you will not be able to use the 
  137. "cc" command, you will have to jump strait to step (8).  Assuming you have 
  138. a second drive and you called your file "hello.c" type the command 
  139.  
  140.     cc hello.c
  141.  
  142. after much clunking of disk drives, you will find that your directory 
  143. contains a file "a.out" as well as "hello.c".  Use the command
  144.  
  145.     list
  146.  
  147. to show you the contents of the directory.
  148.  
  149. 7) Run the file
  150.  
  151.   Once "a.out" has been created you can run the program by typing
  152.  
  153.     a.out
  154.  
  155. you will be amazed by the result, or there again you might not be.  Once 
  156. you are bored with "a.out" remove it from the directory with the command
  157.  
  158.     delete #?.out
  159.  
  160. 8) Create a Makefile
  161.  
  162.   "Real" 'C' programmers rarely us the "cc" command directly, they 
  163. normally use a program called "make".  This file will be different if you 
  164. only have one disk drive.
  165.  
  166. 8a) If you only have one disk
  167.  
  168.   If you only have a single floppy system you will need to create a file 
  169. called "Makefile" the complete file should read
  170.  
  171.     hello : hello.o
  172.          NorthC:bin/blink with $*.blink
  173.  
  174.     .c.o:
  175.          NorthC:bin/NorthC $*.c
  176.          NorthC:bin/A68K -q -g $*.s
  177.          delete $*.s
  178.  
  179.     .asm.o:
  180.          NorthC:bin/A68K $*.asm
  181.  
  182.     .s.o:
  183.          NorthC:bin/A68K $*.s
  184.  
  185. you will need to add the extra items such as ".c.o", ".asm.o" and ".s.o" 
  186. to all your makefiles until you get a second drive.
  187.  
  188. 8b) If you have a second drive
  189.  
  190.   Use "memacs" to create a file called "Makefile" containing
  191.  
  192.     hello : hello.o
  193.             blink with $*.blink
  194.  
  195. this tells "make" how to build the "hello" program.
  196.  
  197. 9) For all machines
  198.  
  199.   Once you have created the "Makefile" we must create a ".blink" file for 
  200. the linker, use the command
  201.  
  202.     NorthC:bin/make $*.blink
  203.  
  204. this will create a file called "hello.blink", now type the command
  205.  
  206.     NorthC:bin/make
  207.  
  208. the machine will now translate your 'C' code into a program.  Use the 
  209. "list" command to look at the directory, it should now contain "hello.o" 
  210. and "hello".  Try the command
  211.  
  212.     hello
  213.  
  214. for some astounding results.  The file "hello.o" is an "object" file, I 
  215. will explain what they are for later.  The "hello" directory also contains 
  216. a "hello.info" icon file, once you have created your "hello" program you 
  217. can run it by double-clicking the "hello" icon in the directory.
  218.  
  219.   It is good practice to always use the "make" program when compiling your 
  220. 'C' programs, if you want to know why read "make.doc".
  221.  
  222. 10) Go again
  223.  
  224.   Edit the "hello.c" file to do something else, get ideas from published 
  225. 'C' programs, public domain programs, friends, and anywhere else you can.
  226.  
  227.   There are a number of example programs on this disk, also look on the 
  228. "NorthC" disk in the "tools" directory, steal all the good ideas from them 
  229. (if you can find any).
  230.  
  231. 11) Get serious
  232.  
  233.   Buy yourself a copy of "Kernigan & Ritchie" or better yet get someone 
  234. else to buy one for you.  I know it is expensive but if you get seriously 
  235. into 'C' you will get your own copy in the end anyway.  It really is the 
  236. best book on 'C' ever written, even if the code layout is a bit weird.
  237.  
  238.   Find a good book for AmigaDOS, I cannot recommend any book here as I 
  239. have yet to find one that doesn't require a degree in computer science to 
  240. understand, however I am sure that some easy to understand books are around.
  241.  
  242.   Read the "libc.doc" file on this disk to see what is currently 
  243. implemented in the NorthC library, read the "Bugs.doc" file especially if 
  244. a program does something you really didn't expect, or if it fails to compile.
  245.  
  246. 12) Multiple files
  247.  
  248.   As a taster for the complexities of programming I will explain about 
  249. compiling large programs that are split over many files, ignore this bit 
  250. if you want.
  251.  
  252.   Once you have a decent sized program you will want to split it over many 
  253. source files, suppose you have a program that is written in the files 
  254. "foo.c" "bar.c" and "baz.asm".  You can compile the files with the command
  255.  
  256.     cc -ofoo foo.c bar.c baz.asm
  257.  
  258. however this will recompile all three files each time you compile the program.
  259.  
  260.   The 'C' world contains four different types of file, source files, 
  261. assembler files, object files and executable files.  You can usually tell 
  262. which category any file comes in by its extension, for example source file 
  263. names end in ".c", for example "foo.c" "this.is.source.c" and so on.  
  264. Object files normally end in ".o", assembler files end in ".s" or ".asm", 
  265. and executable files have no extensions.
  266.  
  267.   The 'C' compilation process contains three stages, first a compiler that 
  268. translates source files into assembler files, then an assembler that 
  269. translates assembler files into object files, and finally a linker that 
  270. combines a number of object files to create an executable.  The "make" and 
  271. "cc" commands will call each of these stages for you, when you type 
  272.  
  273.     cc -ohello hello.c
  274.  
  275. "cc" will first call the compiler, called "NorthC", to create a file 
  276. "hello.s", next "cc" will call the assembler, called "A68K", to create a 
  277. file "hello.o", and finally it will call the linker, "Blink", to make the 
  278. executable "hello", once this is created it will delete the intermediate 
  279. files "hello.s" and "hello.o".
  280.  
  281.   The "make" command tends to leave the object files in the directory.  So 
  282. when we make the program the files "foo.o" "bar.o" and "baz.o" stay 
  283. around.  Now if the user changes "baz.asm" make knows that it must 
  284. reassemble "baz.asm" then link "foo.o" "bar.o" and "baz.o".  It knows that 
  285. it does not need to recompile "foo.c" and "bar.c".
  286.  
  287.   Once you have understood all that you should find a good 'C' book and 
  288. lookup libraries, not to be confused with Amiga libraries, this will get 
  289. you nicely confused again.  I hope that didn't confuse you.
  290.  
  291.   Examine the source code for "cc" in tools, this will show what "cc" can 
  292. do, if you would rather that it did something differently change the file 
  293. "cc.c" make a note of the fact you have changed it at the top of the file 
  294. and recompile it.  The original "cc.c" was hacked together in a single 
  295. evening with just the UNIX "cc" manual for a spec, so I won't mind if you 
  296. 'improve' it, provided you note what changes you have made at the top of 
  297. the source.  If you manage to create a better version please send it to 
  298. me, tell me if I can use it in future versions of NorthC.
  299.  
  300. 13) Share and Enjoy (or Go Stick Your Head in a Pig)
  301.  
  302.   Once you understand 'C' you should share your knowledge, if you have an 
  303. interesting program put it into the public domain or make it shareware, if 
  304. you find learners in 'C' and AmigaDOS explain how to do things, always 
  305. remember that you once knew nothing about computers, even if it was a long 
  306. time ago.  Most of all you should have fun with computers, unless of 
  307. course you are a COBOL programmer.
  308.  
  309. 14) After
  310.  
  311.   If, as the sun slowly sets, you realise that the astounding NorthC disk 
  312. has turned you from a puny BASIC programmer into a towering 'C' wizard you 
  313. should now get a "scheme" interpreter, for example from Fish 149, and a 
  314. copy of "The Structure and Interpretation of Computer Programs" by 
  315. "Ableson & Sussman".  This book is the best introduction to programming I 
  316. have ever read, once you have completed it you will know more about 
  317. programming than most professionals.
  318.  
  319. By the way the factorial function in scheme is
  320.  
  321.     (define (factorial n)
  322.         (if (= n 0) 1
  323.             (* n (factorial (- n 1)))))
  324.  
  325.