Introductions
- Labs are times to be social.
- To help this, let's introduce each other.
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 generating 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 examine 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 (Harley Hahn's Guide to Unix and Linux)
info is a subset of emacs; info info to get started; man man; man is better than Google -- specific to your OS and installation
Looking Ahead
- HW1 is due tonight.
- HW2 won't be assigned until Monday but it is online if you want to start early.
- Monday we will start reviewing C.