Title: CS465 - Unix
1CS465 - Unix
- C Programming (cc/make and configuration control)
2Unix C compilers
- Most Unix system have a C compiler built-in.
Both the cc and the gcc utilities compile and
link C source code. - Syntax
- cc options source(s) -o exefile
- gcc options source(s) -o exefile
- (depending on system)
- If -o is not used, by default the executable
filename is a.out. - To run the program, simply type the name of the
executable file (default a.out).
3cc Example (using a.out)
- cat world.c
- include ltstdio.hgt
- main ()
-
- printf ("Hello World!")
-
- cc world.c
- a.out
- Hello World!
-
4gcc Example (using named exe)
- gcc world.c -o world.exe
- world.exe
- Hello World!
-
5Compiling with Multiple Source Files
- Place function main() in one source code (.c)
file - Place each C function (or set of C functions)
into a separate source code (.c) files - Example Compile and link the code in prog1.c
and prog2.c into the executable file prog. - cc prog1.c prog2.c -o prog
6Separate Compilation of Multiple Source Files (1)
- If you compile each source file separately, it
can be used by whatever program needs it. - The compiler -c option allows separate
compilation of files, creating object code, not
executable code. - Create a header (.h) file for each function file
that will be compiler separately. - Add
- include "f1.h"
- lines to each file that calls a function from
another file.
7Separate Compilation of Multiple Source Files (2)
- Compile each source code file separately into a
compiled object file - cc c f1.c
- cc c f2.c
- cc c main.c
- Link the object files together with the main
program object file into an executable file - cc f1.o f2.o main.o o exename
- Avoids rewriting similar code for each project
8Object File Linking
- Linking the object files together
- Resolves relative addresses of
- variables
- functions
- Creates a single Executable file
9Example
- Write a useful set of Math Functions and put them
in the source file math.c - Create a header file math.h
- Compile math.c into an Object File
- cc c math.c
- The -c option creates object code file math.o by
default - Can use o file.o to save under a different name.
10Example (cont)
- Now suppose file mainfile.c uses the functions in
math.c - Add the line include "math.h" to mainfile.c
- Compile mainfile.c into an Object File
- cc c mainfile.c
- Link the two together and create executable,
myprog - cc mainfile.o math.o -o myprog
- Run using the executable name
- myprog
11Example (variation)
- OR can compile and link in One Step
- cc mainfile.c math.o -o myprog
- And then run
- myprog
12What is make?
- The make command automates the process of
building files that depend on other files. - Typically used for program development
- runs the compiler only when necessary
- uses file access times to decide when it is
necessary - However, make can be used for lots of tasks (not
just for programming).
13What is a Dependency?
- Say that file foo should be rebuilt whenever file
blah is changed. - If blah is newer than foo, we need to rebuild foo
. - Therefore foo depends on blah
14Program Dependencies
- From our compiler example
- myprog is built from mainfile.o and math.o
- mainfile.o is built from mainfile.c
- math.o is built from math.c
- Dependency Graph
- Change in math.c causes
- math.o Outdated
- myprog Outdated
- But there would be no need to recompile
mainfile.c - Use make to determine recompilation requirements!
15make manpage
- NAME
- make - utility to maintain groups of
programs - DESCRIPTION
- The purpose of the make utility is to determine
automatically which pieces of a large program
need to be recompiled, and issue the commands to
recompile them. -
16Makefiles
- make needs a list of rules on how to create the
final target file. Rules include - file dependencies
- instructions on how to build the dependent file
- The rules are located in file
- Default file makefile
- You can also use make f filename
17Rules Format
These MUST be tabs!!!
- target dependencies
- command1
-
- commandn
- target depends on the files listed after the
colon. - commands are Unix commands that build a new
target file.
18Simple Rule Example
- This rule would tell make that the file linecount
depends on the file foo.c - linecount foo.c
- wc l foo.c gt linecount
- Rule says that to build the file linecount, the
command wc l foo.c gt linecount should be run.
19Sample Makefile 1
File anal.c is built by concatonating stats.c and
avg.c. File getio.c is built by concatonating
numsin.c and wordsin.c. The executable is then
built by compiling and linking anal.c and getio.c.
prog.exe getio.c anal.c cc getio.c anal.c o
prog.exe getio.c numsin.c wordsin.c cat
numsin.c wordsin.c gt getio.c anal.c stats.c
avg.c cat avg.c stats.c gt anal.c
20Sample Makefile 2
Given that function main calls function f1, and
function f1 calls function f2. (i.e. f1 is
dependent on f2, and main is dependent only on
f1)
myprog main.o f1.o f2.o cc -o myprog main.o
f1.o f2.o main.o main.c f1.h cc -c main.c
f1.o f1.c f1.h f2.h cc -c f1.c f2.o f1.c
f2.h cc -c f2.c
21Makefile Rules
myprog main.o f1.o f2.o cc -o myprog main.o
f1.o f2.o
- make checks to see if all files are up-to-date
- If myprog is newer than all others, make assumes
everything is up-to-date - If everything is up-to-date, does nothing
- Otherwise make searches the rule for dependencies
22Makefile Rules (cont)
myprog main.o f1.o f2.o cc -o myprog main.o
f1.o f2.o
- Rebuilds out of date dependencies or
sub-dependencies according to other rules in the
Makefile - After all Dependencies are up-to-date the shell
command on Second Line is executed - NOTE Commands must be preceded by a TAB
--- NO SPACES!!!
23make syntax
make -f makefile -f use makefile for
rules Other Flags -n simulate compilation
actions (show actions, but dont do them) -k
ignore errors
24Example of Running make
- cat testmake
- test1 test1.c
- cc test1.c -o test1
- make -f testmake
- cc test1.c -o test1
- test1
- Enter positive whole base 3
- Enter positive whole exponent 3
- Base 3 to the 3 power 27
25Configuration Control
- Protects and controls access to files so that
more than one person is not editing the same file
at the same time. - Often used when multiple people are working on
the same source code file of a program - Can also be used for any other file that multiple
people have access to.
26Configuration Control Programs
- Two most popular
- RCS - revision control system
- SCCS - source code control system
- Our textbook covers SCCS
- Unix in Nutshell covers RCS
27What Configuration Control programs provide
- A collection of tools that lets you
- Put files under configuration control
- Check out one modifiable copy
- Put write locks on updates
- Check out multiple read-only copies
- Check in and document changes
- Print histories
- Merge updates