CS465 - Unix - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CS465 - Unix

Description:

CS465 - Unix C Programming (cc/make and configuration control) – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 28
Provided by: Smallwood
Category:
Tags: commands | cs465 | unix

less

Transcript and Presenter's Notes

Title: CS465 - Unix


1
CS465 - Unix
  • C Programming (cc/make and configuration control)

2
Unix 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).

3
cc Example (using a.out)
  • cat world.c
  • include ltstdio.hgt
  • main ()
  • printf ("Hello World!")
  • cc world.c
  • a.out
  • Hello World!

4
gcc Example (using named exe)
  • gcc world.c -o world.exe
  • world.exe
  • Hello World!

5
Compiling 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

6
Separate 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.

7
Separate 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

8
Object File Linking
  • Linking the object files together
  • Resolves relative addresses of
  • variables
  • functions
  • Creates a single Executable file

9
Example
  • 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.

10
Example (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

11
Example (variation)
  • OR can compile and link in One Step
  • cc mainfile.c math.o -o myprog
  • And then run
  • myprog

12
What 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).

13
What 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

14
Program 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!

15
make 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.

16
Makefiles
  • 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

17
Rules 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.

18
Simple 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.

19
Sample 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
20
Sample 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
21
Makefile Rules
  • This is a Rule

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

22
Makefile 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!!!

23
make syntax
make -f makefile -f use makefile for
rules Other Flags -n simulate compilation
actions (show actions, but dont do them) -k
ignore errors
24
Example 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

25
Configuration 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.

26
Configuration Control Programs
  • Two most popular
  • RCS - revision control system
  • SCCS - source code control system
  • Our textbook covers SCCS
  • Unix in Nutshell covers RCS

27
What 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
Write a Comment
User Comments (0)
About PowerShow.com