Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS465 - Unix C Programming (cc/make and configuration control)"— Presentation transcript:

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

6 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)

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

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

9 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

10 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)

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

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 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.omainfile.o mainfile.c math.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!

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

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 target : dependencies command1 : commandn target depends on the files listed after the colon. commands are Unix commands that build a new target file. These MUST be tabs!!!

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 > linecount Rule says that to build the file linecount, the command “ wc –l foo.c > linecount ” should be run.

19 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 > getio.c anal.c:stats.c avg.c cat avg.c stats.c > anal.c 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.

20 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):

21 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

22 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)

23 $ 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

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


Download ppt "CS465 - Unix C Programming (cc/make and configuration control)"

Similar presentations


Ads by Google