home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gccdocs.lzh / GCCDOCS / GCCSTART.TXT < prev   
Text File  |  1991-07-08  |  41KB  |  372 lines

  1. @CB②PI⇧Fx0œF
  2.             Mike Dorman's "Quick Start" docs for the GNU C compiler, GCC.F
  3.                                                                          
  4.                                                                                         So you want to be a rock and roll star...Sorry, a Byrds
  5.                                                                                 flashback, I apologize.
  6.                                                 So you want to use GNU C.
  7.                                         
  8.                                                                           GENERAL COMMENTS
  9.                                                   
  10.                                                                  Well, although GCC is a *very* good compiler--and free, to
  11.                                                                                    boot--it could be called, rather charitably, "user-hostile." 
  12.                                                                                  I'm writing this document in the hopes of de-mystifying the
  13.                                                                                process of setting-up and using it.  I won't make any warranty
  14.                                                                                   that this document will save your sanity, help you sleep better,
  15.                                                                                     promote world peace, or even not cause you to trash your hard
  16.                                                                                  disk, so don't come crying to me if any of this happens.  That's
  17.                                                                                     the official disclaimer, so let's get down to business.
  18.                                                                  
  19.                                                                                 The GNU C compiler is a wonderful thing, but it was written
  20.                                                                                     on and developed for users of Unix (which is a registered
  21.                                                                              trademark of AT&T) and Unix variants, and so it doesn't
  22.                                                                            necessarily mesh with your ST all that well.  I'll try and show
  23.                                                                                    you how to set it up to work from a command-line-interface (from
  24.                                                                                     here on out, a "CLI") or from the desktop, but depending on your
  25.                                                                                     system resources, one or the other or both may be out of the
  26.                                                                                 question.
  27.                    
  28.                                  SYSTEM REQUIREMENTS
  29.                                                     
  30.                                                                    To begin with, you need *at least* 1MB of ram.  That's not
  31.                                                                                    that stringent a requirement these days, at least not for semi-
  32.                                                                                    serious development, but 1MB is also only going to allow you to
  33.                                                                                    compile *really small* programs.  You best bet is with 2MB or
  34.                                                                                  more of system ram--4MB is really neat, and you would probably
  35.                                                                                   have enough space to have a ramdisk present for temporary files
  36.                                                                                    (GNU C creates a *lot* of them).
  37.                                           
  38.                                                          Next, you need a hard-disk.  There's no way around it--the
  39.                                                                                    program that actually *compiles* code is ~600K, so that did away
  40.                                                                                     with nearly a whole floppy-disk's worth of space right there.  If
  41.                                                                                      you don't have one, you ought to, you would really like it.  Come
  42.                                                                                      back when you have one, because until you do, you're not going to
  43.                                                                                      be able to use GCC at all.
  44.                                     
  45.                                     ✓✓✓GENERAL SETUP INFO
  46.                                                    
  47.                                                                   Assuming you have the hardware required, you need to set up
  48.                                                                                     GCC.  The first setup I'll mention is that for CLI users--I
  49.                                                                                haven't used this in a while, so forgive any inaccuracies you may
  50.                                                                                      find--tell me about them, though, since I will spend some time
  51.                                                                                   updating this document, if necessary--I use GCC myself, and much
  52.                                                                                     of this information is strictly experience-based.  If I'd had
  53.                                                                                  this document, I would have been doing programming *many* months
  54.                                                                                     before I actually started, because I wouldn't have had to spend
  55.                                                                                    so much time sussing out the system.  People who intend to use œF
  56.           GNU C from GEM still need to read the next section on CLI-basedF
  57.                                                                                    setup--there's some concepts discussed there that you still need
  58.                                                                                     to be familiar with.
  59.                               
  60.                                       CLI SETUP
  61.                                                
  62.                                                               There are a bunch of CLIs around there, but some of them are
  63.                                                                                      not acceptable to GCC--the biggest reason being that some
  64.                                                                              (notably PCOMMAND) force the command lines to uppercase, and GCC
  65.                                                                                     uses lower-case parameters all the time.  I can personally
  66.                                                                               recommend GULAM, an excellent CLI with a MicroEmacs-based built-
  67.                                                                                     in editing facility, and MUPFEL, the excellent CLI included with
  68.                                                                                     GEMINI.
  69.                  
  70.                  ✓✓Once you've got your CLI in place and set up to reflect some
  71.                                                                                      of your personal foibles, you need to start customizing it for
  72.                                                                                   GCC.
  73.               
  74.                The first thing you need to do here is find your CLI's
  75.                                                                                equivalent of MS-DOS's AUTOEXEC.BAT.  For GULAM this is the file
  76.                                                                                     GULAM.G.  For MUPFEL, this is MUPFEL.MUP.
  77.                                                    
  78.                                                                   Fire up your favorite editor, set to edit this file.  You
  79.                                                                                   will need to set six environment variables.  Most shells use the
  80.                                                                                     format:
  81.                  
  82.                  ✓✓setenv <variable name> <variable value>
  83.                                                       
  84.                                                                      The environment variables you will nedd to set are:
  85.                                                                   
  86.                                                                                  GCCEXEC
  87.                                      GCC_EXEC_PREFIX
  88.                                              GNULIB
  89.                                     GNUINC
  90.                                     TEMP
  91.                    ✓✓✓✓PATH
  92.                    
  93.                                        GCCEXEC
  94.                                               
  95.                                                              GCCEXEC is the environment variable which points to the
  96.                                                                                 executables of GNU C.  This variable requires that you give the
  97.                                                                                    complete path to where the executables are store, and then append
  98.                                                                                      "gcc-" to that path.  For my system-setup, you'd get:
  99.                                                                
  100.                                                                               setenv GCCEXEC g:\gnubin\gcc-
  101.                                             
  102.                                                            Modify the drive letter and the pathname as you see fit to
  103.                                                                                    make it conform to your system.  This variable points to the
  104.                                                                                 executables so GCC.TTP, the "compiler driver," can find them.
  105.                                                                        
  106.                                                                                                           GCC_EXEC_PREFIX
  107.                                                   
  108.                                                                  GCC_EXEC_PREFIX seems to be functionally equivalent to
  109.                                                                                GCCEXEC.  In fact, it also has to have that silly "gcc-" appended œF
  110.           to the path, just as GCCEXEC does.  I haven't bothered to findF
  111.                                                                                   out if these two variables are redundant.  For my system-setup
  112.                                                                                   you'd end up with:
  113.                             
  114.                                            setenv GCC_EXEC_PREFIX g:\gnubin\gcc-
  115.                                                     
  116.                                                                                            GNULIB
  117.                                              
  118.                                                             GNULIB is the environment variable that points to the object
  119.                                                                                      libraries that contain the compiled startup and execution code
  120.                                                                                   that GCC uses--for instance, the code that comprises the standard
  121.                                                                                      input/output functions in the module "stdio."  It also points to
  122.                                                                                     the files gcrt0.o and crt0.o, the two files that contain the
  123.                                                                                 runtime startup code for every GNU C-compiled program.  Don't let
  124.                                                                                      the tech talk confuse you--let's just say that these are *very*
  125.                                                                                    necessary files.  In the GCC 1.40 release that I uploaded, all
  126.                                                                                   these files were in the file LIBOLB68.LZH.  Without these files,
  127.                                                                                     you're up the creek, so go get them if you don't already have
  128.                                                                                  them.  For my system-setup, you'd have the following:
  129.                                                                
  130.                                                                               setenv GNULIB g:\gnulib
  131.                                       
  132.                                                      Note that this variable doesn't have to have any of the
  133.                                                                                 silly postfixes that GCCEXEC and GCC_EXEC_PREFIX require.  It
  134.                                                                                  *should not* end in a backslash ("\").
  135.                                                 
  136.                                                                                        GNUINC
  137.                                              
  138.                                                             This variable tells GNU C where to find the header files for
  139.                                                                                      the compiler.  These files are the ".h" files that you often see
  140.                                                                                     refered to in the first lines of a program, like this:
  141.                                                                 
  142.                                                                                #include <stdio.h>
  143.                                  
  144.                                                 These header files contain useful definitions of constants
  145.                                                                                    your program may need to know (such as NULL), or prototypes for
  146.                                                                                    various functions (such as printf--these prototypes allow the C
  147.                                                                                    compiler to make sure that you aren't handing some bogus
  148.                                                                             arguments to a function).  They are *very* necessary, also.  In
  149.                                                                                    fact, everything I discuss here is pretty important--this is the
  150.                                                                                     stuff I *know* you need--anything else, well, its omission hasn't
  151.                                                                                      bitten me yet.
  152.                         
  153.                                        For my system-setup, GNUINC is defined like this:
  154.                                                                 
  155.                                                                                setenv GNUINC g:\gnuinc
  156.                                       
  157.                                                      The syntax is the same as GNULIB, not requiring any of the
  158.                                                                                    funky little additions that GCCEXEC and GCC_EXEC_PREFIX do.
  159.                                                                      
  160.                                                                                                              TEMP
  161.                                             
  162.                                                            As I said before, GCC produces *lots* of temporary files, so
  163.                                                                                      that the various peices of the compiler can communicate and pass œF
  164.           their processed information to the next link in the greatF
  165.                                                                              compiler-chain.
  166.                          
  167.                                         GNU C *deperately* needs to be told where to put these
  168.                                                                                temporary files--otherwise it'll probably attempt to stash them
  169.                                                                                    in places that don't exist on your system--or maybe you do have
  170.                                                                                    a /gnu/gcc/cross-compile/temp directory, and I don't know what
  171.                                                                                   I'm talking about.  :^)
  172.                                  
  173.                                                 If you have a ramdisk, this is probably the place to put
  174.                                                                                  your temporary files, as doing that will make GNU C *fly*. 
  175.                                                                                Unfortunately, that upgrade to 4MB is a ways away for me, and
  176.                                                                                  since I need most of my 2.5 MB for GNU C to work in, I have to
  177.                                                                                   send temporary files to my hard drive.  For my system-setup, TEMP
  178.                                                                                      is set to:
  179.                     
  180.                                    setenv TEMP g:
  181.                              
  182.                                             Notably, you *cannot* have this variable end in a backslash
  183.                                                                                     and still expect GCC to function.  If TEMP is set incorrectly,
  184.                                                                                   you'll get some interesting, but somewhat obscure, error messages
  185.                                                                                      when you try to compile.  You might want to try it, just so
  186.                                                                                you'll see them, so you can recognize them in the future, because
  187.                                                                                      this one variable will probably bite you more than any other.
  188.                                                                        
  189.                                                                                                                PATH
  190.                                             
  191.                                                            This may be the most important environment variable of them
  192.                                                                                     all.  Despite the existence of GCCEXEC and GCC_EXEC_PREFIX, GNU C
  193.                                                                                      still uses the old, reliable PATH variable to find some of its
  194.                                                                                   files.  For my system-setup I have PATH set up this way:
  195.                                                                   
  196.                                                                                  setenv PATH c:\;g:\gnubin
  197.                                         
  198.                                                        There are some interesting things about PATH that you should
  199.                                                                                      know.  First, GEM *really*, **really** wants your boot device to
  200.                                                                                     be the first entry in PATH, hence the "c:\" as my first entry. 
  201.                                                                                    The second item is this--GEM uses PATH to help itself find
  202.                                                                               resource (.rsc) files for GEM applications.  If you set you path
  203.                                                                                     correctly, you can move *all* of your resource files to one
  204.                                                                                directory--possibly eliminating some clutter.  The last comment
  205.                                                                                    I'll make is a little more general--PATH, GNULIB and GNUINC can
  206.                                                                                    all point to more than one particular directory.  All you have to
  207.                                                                                      do is separate the different directories with commas (",") or
  208.                                                                                  semicolons (";").  BE WARNED!  In GULAM, and I think MUPFEL,
  209.                                                                                 semicolons are used to "stack" commands, so you will need to
  210.                                                                                 enclose any variables which include them in quotes.  For example:
  211.                                                                            
  212.                                                                                           setenv PATH "c:\;g:\gnubin"
  213.                                           
  214.                                           ✓✓✓✓GEM SETUP
  215.                                                
  216.                                                               You have probably taken the harder road for the nonce--with
  217.                                                                                     the right tools (which I should be able to upload to GEnie, RSN, œF
  218.           just as soon as Ian Lepore decides that they've been beta-testedF
  219.                                                                                     enough), programming from the desktop can be a breeze, but right
  220.                                                                                     now, it requires some special tools that aren't as user-friendly
  221.                                                                                     as the ones I'm using.
  222.                                 
  223.                                                First, you need some way to set environment variables.  As
  224.                                                                                    the preceeding text (marked for CLI users, but you did read it,
  225.                                                                                    didn't you, like I recommended?) might indicate, environment
  226.                                                                                 variables are what GNU C lives and dies by.  Unfortunately,
  227.                                                                                though GEM will use environment variables, it doesn't give you an
  228.                                                                                      easy way to set them.
  229.                                
  230.                                               NeoDesk, by Gribnif Software, supports them, and if you
  231.                                                                                 don't have that, there's an auto-folder program called ENVIRON
  232.                                                                                   (it should be on GEnie) which will set them as well.  You need to
  233.                                                                                      get one of these programs, read the documentation and set the
  234.                                                                                  variables that I spent the last few pages describing, but in
  235.                                                                                 accordance with your setup, of course.
  236.                                                 
  237.                                                                               BASIC COMPILING EXAMPLES
  238.                                                       
  239.                                                                      Well, once you've gotten all those variables set, you're
  240.                                                                                  ready to start compiling stuff.
  241.                                          
  242.                                                         If you're using a CLI, you'll need to type gcc (don't hit
  243.                                                                                   enter yet!), and then wait a minute while I attend to the GEM
  244.                                                                                  people.  If you're using GEM, you need to double-click on
  245.                                                                              GCC.TTP, so that it gives you a dialog box so you can input
  246.                                                                                command line options.
  247.                                
  248.                                               There are a bunch of command line options, all well-
  249.                                                                              documented in the GCC ST documentation.  By the time you need the
  250.                                                                                      others, though, you will probably know enough to figure them out
  251.                                                                                     for yourself (a cop out, I know, but it's late, and I promised
  252.                                                                                   myself that I'd write this before I went to bed).  For the
  253.                                                                               moment, I'll show you some of the simpler, but most important,
  254.                                                                                   ones.
  255.                
  256.                For example, let's say you have the file wombat.c, and you
  257.                                                                                    want to compile it.  You would probably want to type the
  258.                                                                             following command line in (omitting the gcc at the beginning if
  259.                                                                                    you're using the desktop):
  260.                                     
  261.                                                    gcc -v -O -o wombat.ttp wombat.c
  262.                                                
  263.                                                               The "-v" parameter specifies *verbose* mode.  This means
  264.                                                                                  that gcc and all the compiler programs will indicate that they're
  265.                                                                                      running, and give you the version numbers, and such.  This isn't
  266.                                                                                     necessary, but it's useful, just to watch the process involved.
  267.                                                                          
  268.                                                                                         The "-O" parameter specifies *optimize* mode.  In this mode,
  269.                                                                                      GCC will try to produce the smallest, fastest code it can. 
  270.                                                                                Again, this isn't essential, but it *is* useful.
  271.                                                            œF
  272.                The "-o" parameter (remember, this stuff is case-sensitive,F
  273.                                                                                     which means using GCC from the desktop with TOS 1.0 *will not*
  274.                                                                                   work) has an attachment--in this case, "wombat.ttp".  This
  275.                                                                               parameter tells GCC what the output file's name will be.  It
  276.                                                                                 isn't necessary, but without it, GCC will produce a file named
  277.                                                                                   "a.out".  While this will be a perfectly fine executable, since
  278.                                                                                    it doesn't end in .tos, .ttp or .prg, TOS won't know it's meant
  279.                                                                                    to be executable, and will, therefore, stare at you blankly if
  280.                                                                                   you try to run it.
  281.                             
  282.                                            The last parameter, "wombat.c", tells GCC what file to
  283.                                                                                compile.  You can include a path, such as "d:\wombat\wombat.c" if
  284.                                                                                      you choose--this is most useful from GEM, since you may not have
  285.                                                                                     the source file in the same directory as GCC.
  286.                                                        
  287.                                                                       All this, though, only shows you how to do text-based, non-
  288.                                                                                     GEM programs.  Thos are fine for IBM users, but we've got a
  289.                                                                                built-in GUI, so you'll probably want to write GEM programs
  290.                                                                                before long.  Compiling those requires more parameters, such as
  291.                                                                                    in the example:
  292.                          
  293.                                         gcc -v -O -o wombat.prg wombat.c -lgem
  294.                                                      
  295.                                                                     This command line differs from the first in two respects. 
  296.                                                                                    First, the target file is named .prg, so it will run with GEM and
  297.                                                                                      mouse support and all that.
  298.                                      
  299.                                                     The second difference is the "-lgem" parameter.  This tells
  300.                                                                                     the compiler to link in the library with all the object files
  301.                                                                                  that support GEM calls--probably named "gem.olb." Actually, the
  302.                                                                                    parameter is "-l", with "gem" denoting the name of a compiler
  303.                                                                                  library--one of those pesky, huge, ".olb" files.
  304.                                                           
  305.                                                                          Assuming that you'e got everything set up right, you should
  306.                                                                                     have a program on your hands.  Run and enjoy!
  307.                                                        
  308.                                                                                       MORE ADVANCED COMPILING
  309.                                                       
  310.                                                                      There's one other thing that you ought to know about.  One
  311.                                                                                    of C's features is the ability to compile modules of programs
  312.                                                                                  separately, and then link those together.  In this way, you can
  313.                                                                                    avoid having one *huge* ".c" file, by putting routines in other,
  314.                                                                                     smaller, ".c" files.
  315.                               
  316.                                              To do this properly, there are two steps you have to go
  317.                                                                                 through.
  318.                   
  319.                   ✓✓✓First, you have to compile the separate modules into ".o"
  320.                                                                                   files.  You do this with the command line:
  321.                                                     
  322.                                                                    gcc -c -v -O wombat.c
  323.                                     
  324.                                                    In this example, the "-c" flag tells the compiler to just
  325.                                                                                   compile the file, not to link it--which mashes it in with a œF
  326.           bunch of other files, an ugly process which is necessary toF
  327.                                                                                create an executable file.  This command line *should* produce a
  328.                                                                                     file with the same name as the ".c" file, but an extension of
  329.                                                                                  ".o."
  330.                
  331.                Once you have done this procedure to all your smaller
  332.                                                                               routines, you then need to tell the compiler to link them to your
  333.                                                                                      main program module.  You use this command line for that:
  334.                                                                    
  335.                                                                                   gcc -v -O -o wombat.prg wombat.c smut.o lewd.o -lgem
  336.                                                                    
  337.                                                                                   This will automagically link the files "smut.o" and "lewd.o"
  338.                                                                                      (or whatever the names of the object files you created in the
  339.                                                                                  above example are) into the file wombat.prg.  It will then link
  340.                                                                                    in any necessary files from the GEM library.  Hopefully, these
  341.                                                                                   steps will produce your first modularly-coded C program.
  342.                                                                   
  343.                                                                                                        FINAL NOTES
  344.                                                 
  345.                                                                Well, I've listened to the same CD four times typing this. 
  346.                                                                                     I hope there's not too many inaccuracies, typos, blatant lies or
  347.                                                                                     spelling mistakes, but I'm sure there will be--it's late and I'm
  348.                                                                                     sleepy.  In the event that you find something unclear,
  349.                                                                           confusing, too brief or outright wrong, please leave mail for me
  350.                                                                                     at any of these electronic addresses--I want to make this
  351.                                                                              document easy to use, and helpful, to boot, and am willing to
  352.                                                                                  revise it if it doesn't start out that way:
  353.                                                      
  354.                                                                     MDORMAN1@UA1VM.UA.EDU on the Internet.
  355.                                                                     mike.dorman on BIX.
  356.                                                  M.DORMAN2 on GEnie.
  357.                                   
  358.                                                  On BIX I am active in the atari.st conference, and messages
  359.                                                                                     left in the c.language topic will get to me, as will e-mail.  On
  360.                                                                                     GEnie, messages left in the GNU C topic--I don't remember the CAT
  361.                                                                                      and TOP numbers--will get to me, as will e-mail.
  362.                                                           
  363.                                                                          Please remember, though, that this isn't meant to be a
  364.                                                                                tutorial on programming in C on the ST--better people than I have
  365.                                                                                      written entire books about that.  This file, will, I hope, make
  366.                                                                                    it easier to use GNU C for this purpose, though.
  367.                                                           
  368.                                                                          Mike.
  369.                     
  370.                                    Typed between the hours of 11 pm July 3, 1991 and 1am July
  371.                                                                                    4, 1991 on WordPerfect for the ST.  All the spelling errors are
  372.                                                                                    the computer's fault. P