Compiling and Using UNIX
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Development Process
- In C, we use the same
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?
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). Most 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" and "Hello" 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, 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.051/
io:~/cse271.051% cd ..
io:~% cd .
io:~% cd ~
io:~% cd /home/bdd3
io:~%
UNIX Commands 2
- List directory contents
io:~/cse271.051% ls
hw1/
io:~/cse271.051% ls -l
total 2
drwxr-xr-x 2 bdd3 ugrad 512 Jan 19 01:16 hw1/
io:~/cse271.051% ls ..
Desktop/ cse271.051/ nsmail/ test.c
Mail/ mail/ test* test.c~
- Deleting files
Be careful about rm -rf!