CS465 - Unix C Programming (cc/make and configuration control)
-
date post
15-Jan-2016 -
Category
Documents
-
view
229 -
download
0
Transcript of CS465 - Unix C Programming (cc/make and configuration control)
![Page 1: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/1.jpg)
CS465 - Unix
C Programming (cc/make and
configuration control)
![Page 2: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/2.jpg)
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).
![Page 3: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/3.jpg)
cc Example (using a.out)
$ cat world.c#include <stdio.h>main (){ printf ("Hello World!");}$ cc world.c$ a.outHello World!$
![Page 4: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/4.jpg)
gcc Example (using named exe)
$ gcc world.c -o world.exe$ world.exeHello World!$
![Page 5: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/5.jpg)
• 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
Compiling with Multiple Source Files
![Page 6: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/6.jpg)
• 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.
Separate Compilation of Multiple Source Files (1)
![Page 7: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/7.jpg)
• 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
Separate Compilation of Multiple Source Files (2)
![Page 8: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/8.jpg)
• Linking the object files together
– Resolves relative addresses of:
• variables
• functions
– Creates a single Executable file
Object File Linking
![Page 9: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/9.jpg)
• 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.
Example
![Page 10: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/10.jpg)
• 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
Example (cont)
![Page 11: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/11.jpg)
• OR can compile and link in One Step:
$ cc mainfile.c math.o -o myprog
• And then run:
$ myprog
Example (variation)
![Page 12: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/12.jpg)
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).
![Page 13: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/13.jpg)
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
![Page 14: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/14.jpg)
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: myprog
math.o mainfile.o
mainfile.cmath.c
Program Dependencies
• 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!
![Page 15: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/15.jpg)
NAME make - utility to maintain groups of programs
DESCRIPTIONThe 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.
make manpage
![Page 16: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/16.jpg)
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
![Page 17: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/17.jpg)
Rules Format
target : dependencies
command1
:
commandn
target depends on the files listed after the colon.
commands are Unix commands that build a new target file.
These M
UST be tabs!!
!
![Page 18: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/18.jpg)
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 > linecount
• Rule says that to build the file linecount, the command “wc –l foo.c > linecount” should be run.
![Page 19: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/19.jpg)
prog.exe: getio.c anal.c cc getio.c anal.c –o prog.exe
getio.c: numsin.c wordsin.ccat numsin.c wordsin.c > getio.c
anal.c: stats.c avg.ccat avg.c stats.c > anal.c
Sample Makefile #1File 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.
![Page 20: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/20.jpg)
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
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):
![Page 21: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/21.jpg)
myprog: main.o f1.o f2.o
cc -o myprog main.o f1.o f2.o
• This is a Rule:
target dependencies
• 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
Makefile Rules
![Page 22: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/22.jpg)
myprog: main.o f1.o f2.o
cc -o myprog main.o f1.o f2.o
target dependencies
•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!!!
Makefile Rules (cont)
![Page 23: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/23.jpg)
$ make [-f makefile]
-f use makefile for rules
Other Flags:-n simulate compilation actions
(show actions, but don’t do them)-k ignore errors
make syntax
![Page 24: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/24.jpg)
Example of Running make
$ cat testmaketest1: test1.c cc test1.c -o test1$ make -f testmakecc test1.c -o test1$ test1Enter positive whole base: 3Enter positive whole exponent: 3
Base 3 to the 3 power = 27$
![Page 25: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/25.jpg)
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.
![Page 26: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/26.jpg)
Configuration Control Programs
• Two most popular:
– RCS - revision control system
– SCCS - source code control system
• Our textbook covers SCCS
• Unix in Nutshell covers RCS
![Page 27: CS465 - Unix C Programming (cc/make and configuration control)](https://reader036.fdocuments.in/reader036/viewer/2022062305/56649d7e5503460f94a60d12/html5/thumbnails/27.jpg)
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