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

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
1 The Makefile Utility ABC – Chapter 11,
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 CS 201 Makefile Debzani Deb. 2 Remember this? 3 What is a Makefile? A Makefile is a collection of instructions that is used to compile your program.
Introduction to Make Updated by Prasad Spring 2000.
Guide To UNIX Using Linux Third Edition
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 9.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
UNIT 13 Separate Compilation.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
The Make utility. Motivation Small programs all in single cpp file “Not so small” programs : Many lines of code Multiple components More than one programmer.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Tools – Ant-MakeEtc 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Tools 12 – Hamcrest 10/02/
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
The make utility (original presentation courtesy of Alark Joshi)
Large Program Management: Make; Ant
Automating Builds with Makefiles
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Large Program Management: Make; Ant
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
CS1010 Programming Methodology
CS1010 Programming Methodology
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Makefiles and the make utility
Large Program Management: Make; Ant
CSE 390 Lecture 8 Large Program Management: Make; Ant
CMPSC 60: Week 4 Discussion
Large Program Management: Make; Ant
SCMP Software Development Spring 2018 James Skon
Writing Large Programs
Large Program Management: Make; Ant
Build Tools (make) CSE 333 Autumn 2018
Appendix F C Programming Environment on UNIX Systems
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Makefiles and the make utility
Makefiles, GDB, Valgrind
CSE 390 Lecture 8 Large Program Management: Make; Ant
SCMP Software Development Spring 2018 James Skon
Large Program Management: Make; Ant
Large Program Management: Make, Ant
Presentation transcript:

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

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

cc Example (using a.out) $ cat world.c #include main () { printf ("Hello World!"); } $ cc world.c $ a.out Hello World! $

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

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

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)

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)

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

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

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)

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

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

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

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!

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

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

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

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.

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.

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

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

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)

$ 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

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 $

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.

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

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