Development Process
- In C, we use the same
design, edit, compile,
execute
process as any other language, or development platform -- just the tools are different.
- Use gcc -o prog file.c instead of the a.out from our first example
- When compiling multiple files, use multiple steps
% gcc -c file.c
% gcc -c file2.c
% gcc -o prog file.o file2.o
Explain the difference between compiling and linking.
What do each of these steps do?
Makefiles
- Normally we automate all of this with a Makefile, such as:
prog: file.o file2.o
<TAB>gcc -o prog file.o file2.o
file.o: file.c
<TAB>gcc -c file.c
file2.o: file2.c
<TAB>gcc -c file2.c
Note that the tabbed lines are identical to the earlier page. TAB is
essential! Often a problem when cutting and pasting. What does
make/Makefile actually do? Show dependencies and rules (could be multiple
rule lines).
Especially useful for larger programs with even more source files.
Now what would you need to do if you wanted to add debugging? Or change
compiler? Or add optimization?
Better Makefiles
CC=gcc
CFLAGS=-O2 -g -Wall
LFLAGS=-g -Wall
prog: file.o file2.o
${CC} ${LFLAGS} -o prog file.o file2.o
file.o: file.c
${CC} ${CFLAGS} -c file.c
file2.o: file2.c
${CC} ${CFLAGS} -c file2.c
clean:
rm -f *.o
rm -f prog
make supports variables so we can use them to make future changes simpler.
clean helps when you want to compile from scratch (e.g., after changing
flags)
Compiling under Emacs
- M-x compile -- Opens a second window within emacs, asks for compile command
- C-x ` -- Goes to the next line reported to have an error
Using UNIX
- Reminder: How to find information about UNIX commands/utilities:
- Type man <program> for any system command, most utilities
- Type info <program> for any GNU utility or program (and man pages!)
- Check your reference books
- The rest: Google, TA, instructor, etc.
- What if you don't know the name of the program?
- man -k <keyword>
- also check your text (UNIX in a Nutshell)
info is a subset of emacs; info info to get started; man man; man is better than Google -- specific to your OS and installation
Logging in and out
- Use ssh instead of telnet (even when permitted, as ssh is more
secure)
- Use your ssh client to connect to a CSE/ECE
Sun
- On our Suns (and Linux) your default shell will be bash
(Bourne-Again Shell). Modern shells, including bash, offer:
- tab completion -- just type the first few letters and press tab to complete the command name
- command history -- use the up and down arrows to select from your history of past commands
- emacs-like keybindings -- C-a, C-e, C-t, C-b, C-f, C-p, C-n all same
- When complete, type exit. Some shells will also allow CTRL-D (EOF) to signal the end of the session.
I use tcsh instead of bash because that is what I learned; again, like editors, shell choice is often a 'religious' issue.
Comparison to DOS/Windows
- Some ideas are similar, such as a file structure of nested folders
- Differences
- UNIX filenames are case-sensitive, so "hello.txt" and "Hello.txt" are
not the same.
- UNIX pathnames use a slash (/) as a separator, not a backslash (\) as in DOS/Windows. The backslash is used to escape special characters just like in most programming languages.
- UNIX has no "drive names" like A, C, D. Instead, the entire filesystem is
mapped under a single root directory "/"
- Often the current directory is not in user's path by default. Need, for example, ./progname
- All files and directories belong to some user, and have access permissions.
- There is a special "root" account for system administration (separate
from regular users), which
can read and write essentially anything, anywhere, regardless of access permissions.
UNIX Commands 1
- Where are you?
tritan 2% pwd
/home/brian/WWW-data/course/cunix
- Who are you?
- Moving to another directory
io:~% cd cse271.071/
io:~/cse271.071% cd ..
io:~% pwd
/home/bdd3
io:~% cd .
io:~% cd ~
io:~% cd /home/bdd3
io:~%
PWD=Print Working Directory
CD=Change Directory
UNIX Commands 2
- List directory contents
io:~/cse271.071% ls
hw1/
io:~/cse271.071% ls -l
total 2
drwxr-xr-x 2 bdd3 ugrad 512 Jan 19 01:16 hw1/
io:~/cse271.071% ls ..
Desktop/ cse271.071/ nsmail/ test.c
Mail/ mail/ test* test.c~
- Deleting files
Be careful about rm -rf!
LS=LiSt (files or contents of a directory)
RM=ReMove
Viewing Files
You can always open a text file in emacs to view it. However, there are
other ways --
cat,
less, and
more are typical
choices.
tritan 2% cat -n test.c
1 int a;
2 int main (sdfsdf a)
3 {
4 int b;
5 }
tritan 2%
less and
more are called
pagers -- they display a
large file, one page (screenful) at a time.
C Programming
- There are many more useful UNIX utilities that we will discuss
in the future, but let's consider building one.
- We just saw that cat can display the contents of a
file.
- Let's write our own version in class. Since cat reads from files,
we'll need to use the open(2), read(2), and close(2) system calls.
- Note that the man system also has entries for system calls and
standard library functions as well as for programs.
Note the man page sections for the system calls.
Use man -s to specify which section of the manual pages to
search.