editing the source code
Unix has at least four different text editors. The
most convenient one is emacs.
Emacs is extremely powerful, it has nice features like
- syntax highlighting,
- auto-indentation,
- debugging support
which help programming a lot. If you don't know emacs yet,
it might be a good idea to learn it. Emacs has a built in
tutorial. Experienced users spend
most of their time in emacs (even for email, unix shell commands, etc.)
Alternative editors are vi, which takes
some time getting used to, and textedit -
a very simple editor. The editor pico might
be a good choice for people new to unix, it's very similar to the editing
mode of the email program pine.
You can start emacs as follows:
compiling and running your program
You should try out one of the example programs, let's say the
hello-world program. Here's a step by step explanation of how
to compile the program:
- type the program (or copy it from the web) and save it as
"hello.c". I'm using emacs to enter the program:
(courses2){biermann}[43]% emacs hello.c
- call the compiler from an unix shell - if you're not sure how
to do this, just quit your editor and write:
(courses2){biermann}[44]% gcc hello.c
- the compiler is generating an executable with the default
name "a.out". Your shell dialogue should look like:
(courses2){biermann}[45]% ./a.out
hello, world!
(courses2){biermann}[46]%
some more details
Actually, gcc does multiple steps to create an executable from your
source code. At first, your program was compiled into object code, then
your code is linked to the libraries in order to create an executable.
You can control these these steps separately:
- We can compile the source code without actually linking it, using
the -c options of the compiler. Gcc is generating a file called
"hello.o" for the object code:
(courses2){biermann}[56]% gcc -c hello.c
- In order to link the code, we're just calling gcc with the object
file(s) to link. Also, we're providing a name for the executable
with the compiler option -o:
(courses2){biermann}[57]% gcc hello.o -o hello
- We can start our program "hello" by typing it's name in the command
line. You can also set up your path in such a way, that you don't
need to type the beginning "./"
(courses2){biermann}[58]% ./hello
hello, world!
(courses2){biermann}[59]%
assignment
- Try to run at least three of the example programs in class.
Use a computer platform, you're familiar with. (Visual C++
should be able to compile the programs under Windows).
- Log onto your unix accounts and try the hello-program.
- Write a program of your own, use some variables and the input
and output commands. You can modify the "circle-area" example.