Introduction to Programming - Lecture 1 by KDS
Kurt Schwehr
Copyright (C) 2003
kdschwehr _at_ ucsd dot edu
Nov 2003
http://schwehr.org/
Introduction to Programming in Unix Like Environments
CONTENTS
Purpose
This lecture is designed to get a student going on learning to program
in a Unix like enviroment. The concepts apply to Microsoft Windows
with the Cygwin package and Mac OS X in addition to Sun Solaris, SGI
Irix, Linux, and the many BSD's. Basically any POSIX complient
Operating System. If you don't know all the buzz words, don't worry.
Just about any Major OS released after the year 2000 will do!
NOTE: This document will NOT help you for Mac OS 9 or older.
Hopefully by the end of this lecture, you will be in a position that
use can learn C, C++, and/or Python on your own or in future
lectures. It is often the case that students are fine learning a
language from a text book, but get easily tripped up on all the tools
and programs that are needed to edit, compile, debug, and run programs.
Program descriptions
This section is a quick list of the programs that we will be using in
this lecture:
- bash - A 'shell'. Provides the command line interface that
you will use.
- cygwin - A package for microsoft windows that provides a unix
link operating environment.
- emacs - Super powerful text/programming editor and much more.
- gcc - GNU Compiler Collection (including java and C++) or GNU C Compiler
- gdb - GNU Debugger. Find out what is happening inside of your programs.
- g++ - GNU C++ compiler
- make - GNU make is a tool for automating the build process
for more complicated programs.
- python - My personal favorit for a scripting language.
Cygwin - a unix link environment for MS Windows
(Based on contributions by Katie P. and Katie G.)
NOTE 1: This works on Windows 2000 and XP. If you are using Windows
3.x, 95, 98, or NT, step one is to go buy a new computer or upgrade
your computer to a newer version of windows (or better yet, install
Linux or FreeBSD).
Installing cygwin on your windows machine:
Go to the cygwin website: http://www.cygwin.com/xfree/
(NOTE: You can also search for cygwin on google.com.)
Click on the install cygwin/Xfree86 now button.
*Important* disable your automatic virus detector (ie. Norton) before
you start the installation process. It seems to hang up the set-up
wizard for some reason. You can re-enable it once you are done
installing.
Run the set-up wizard.
Follow the instructions about where to save files, download from
internet, etc.
When you get to the choose download site screen, choose one that is
close to you. I used ftp://nas.nasa.gov, and that seemed to work
fine.
Next, you will see the Select packages screen.
You will need to select the g++/gcc compiler, gdb, make, and python
from the Developement subgroup, emacs with X11 support from the editor
group, and anything else you would like installed. Picking emacs with
X11 will automatically make the installer setup X11 for you.
Once the installation is done (it could take a while), go to the
directory where you told the install wizard to save your files. Go to
cygwin\usr\X11R6\bin\ doubleclick on startxwin to open up an xterm
window. I made a shortcut for this one on my desktop. If you don't
have a \usr folder, then Xfree86 probably didn't get installed. Go
back to the set-up wizard and make sure it was checked on the select
packages screen.
Now, if everything went OK, you should be able to start up cygwin and
log into other machines including sioviz. If your xterm window is
open, you are good to go!
Mac OS X
These instructions are for Panther (Mac OS X 10.3) or newer. Older
versions will need to use the Apple X11 Beta 3 or XDarwin. You are on
your own for that.
The first step is being able to work with an SGI remotely across a
network with a Mac on your desk or somewhere else on campus. This is
easy to do and works with many (but not all) of the SGI applications.
You will need to install Apple's X11 (X-Windows) server (currently
Beta-3 version) and you will also want to install fink/fink
commander. Here are some quick notes on how to do that.
http://apple.com/
Go to "Software"
Get and Install the developer tools/make sure they are installed
Applications/Installers
double click the devtools installer
http://fink.sourceforge.net/
Downloads - binary installer
Double click the fink SOME-VERSION-NUMBER installer.pkg
Use all the defaults
The installer should edit your command line to add this line to your
.cshrc file.
source /sw/bin/init.csh
Install fink commander
http://finkcommander.sourceforge.net/
Download the disk image
Open the Fink Command disk and drag fink commander into applications
Drag it from applications to your dock
Run fink commander
Install system-xfree86 (this is for apple X11)
Allow access to unstable commands (they usually work fine!)
Run fink commander
Fink Commander -> preferences -> fink
Check "Use unstable packages" and "Use unstable cryptography packages"
Ok
You then can install all sorts of great open source programs on your
Mac really easily.
TODO:
* Make sure that you install the X11 version of emacs from fink. Should
be called something like: "emacs21" or some newer version number.
* Change your shell to be /bin/bash!
Starting Emacs
Now you are ready to start editing files. First run emacs from the
command line:
emacs &
The ampersand (&) means to run it in the background so that you get
your command line back. Now open a file. You can do this with either
the drop down file menu or with the keyboard short cut:
C-x C-f
Traslated, that means hold down the control key and press 'x'. Then
while still holding down control, press 'f'. You will then see
something like this at the bottom of the window:
Find file: ~/
Enter 'simple.c' and press return. Now type in the following program:
#include // for printf
#include // for EXIT_SUCCESS
int
main (int argc, char *argv[])
{
printf ("Hello world\n");
return (EXIT_SUCCESS);
}
Now save the file with File->Save (current buffer) or the keys:
C-x C-s
Compiling
Now we want to compile the program. Type the following where 'M' is
the escape key (called meta) on some older unix workstations:
M-x compile
So that translates to the following keys:
ESC x c o m p i l e
Now hit ender and you will see:
Compile command: make -k
Delete the 'make -k' and replace it with:
gcc -g -Wall -o simple simple.c
Now press enter. This will build your program. A new window pops up:
cd /Users/schwehr/Desktop/IntroProgramming/
gcc -g -Wall -o simple simple.c
Compilation finished at Sat Nov 29 10:21:05
What just happened? We ran the GNU C compiler called 'gcc'. We gave
it a couple flags. Here is what they do:
-g Add debugging information to the executable
-Wall Turn on all of the compiler warnings (ALWAYS use this flag)
-o simple What output file to write the executable to.
simple.c The source file
Now we should run the program. Go back to your terminal/shell window
and run the command:
./simple
And you should see:
> ./simple
Hello world
>
Now, we should see what happens if there is an error in the code. Add
this line between the { and the printf:
int a==1;
Now recompile with "M-x compile" and you will see an error message:
cd /Users/schwehr/Desktop/IntroProgramming/
gcc -g -Wall -o simple simple.c
simple.c:8: syntax error, missing `;' after `a'
cpp-precomp: warning: errors during smart preprocessing, retrying in basic mode
Compilation exited abnormally with code 1 at Sat Nov 29 10:31:36
Emacs can take us to the error by using the "next-error" command:
C-x `
That's hold down control, press 'x', let go of control, finally press
the back quote key (usually on the top left of the keyboard). That
should take you right to the line we just added. Now delete the extra
'=' that we put in so that the program compiles:
int a=1;
Now we get a 'warning' about a variable not being used, with is not
fatal. The program is still built:
cd /Users/schwehr/Desktop/IntroProgramming/
gcc -g -Wall -o simple simple.c
simple.c: In function `main':
simple.c:8: warning: unused variable `a'
Compilation finished at Sat Nov 29 10:35:20
Debugging
Now that we have built our first program, we are ready to try out the
debugger. In emacs, start the debugger like this:
M-x gdb
You will be prompted at the bottom for how to run the debugger. Add
our simple program at the end, so it looks like this:
Run gdb (like this): gdb simple
You now will get a window with the debugger that ends like this:
(gdb)
In this window, let's set a break point at the beginning of the
program:
break main
Now run the program
run
One window will now show your code with a small arrow pointing at the
'int a=1' line, while the gdb window will show something like this:
(gdb) break main
Breakpoint 1 at 0x1d74: file simple.c, line 8.
(gdb) run
Starting program: /Users/schwehr/Desktop/IntroProgramming/simple
[Switching to process 602 thread 0xf07]
Breakpoint 1, main (argc=1, argv=0xbffffb48) at simple.c:8
(gdb)
Now we can step through the program using the next command:
(gdb) next
The arrow should now be pointing at the printf line. We can see the
result of the last line by printing the value of the 'a' variable: