Automating Builds with Makefiles C Programming in a

Automating Builds with Makefiles C Programming in a Linux Environment
![Compiling Your Program helper. c: coord. c: #include<stdio. h> double get. Double(char [], double); Compiling Your Program helper. c: coord. c: #include<stdio. h> double get. Double(char [], double);](http://slidetodoc.com/presentation_image/b27ad560b72b2b7d60f788753d92b319/image-2.jpg)
Compiling Your Program helper. c: coord. c: #include<stdio. h> double get. Double(char [], double); double dist(double x, double y); int main() { // Read the (x, y) coordinates of a 2 D point // where each coordinate is in [-10, 10] // and compute the distance of (x, y) // from the origin. double x = get. Double("Enter the x value: ", -10, 10); double y = get. Double("Enter the y value: ", -10, 10); double distance = dist(x, y); printf("Distance from origin is %. 2 lfn", distance); } Compile and run: % c 99 coord. c helper. c -o coord -lm %. /coord OR #include<stdio. h> #include<math. h> double dist(double x, double y) { double d = sqrt(pow(x, 2) + pow(y, 2)); return d; } double get. Double(char prompt[], double min, double max) { double read. Value; do{ printf("%s", prompt); scanf("%lf", &read. Value); if(read. Value < min) printf("Value must be >= %lfn", min); if(read. Value > max) printf("Value must be <= %lfn", max); } while(read. Value < min || read. Value > max); return read. Value; } % c 99 coord. c helper. c -lm %. /a. out -lm link with math library

C compilation gcc -std=c 99 <sourcefile>. c Flags: • -g: include debugging info • -o <output>: specifies the name of the output file • gcc –std=c 99 prog. c –o prog • Executable is prog, not a. out, so to run program: . /prog • -c: build the object file (produces. o file)

Separate Compilation % gcc –c helper. c % gcc –c coord. c Now link: % gcc –o coord Compile separately. If one file changes, only recompile that one. helper. o coord. o –lm Note that the executable is coord. To run: %. /coord We will automate the build with a makefile. link with math library

make Utility & Makefiles • Unix tool make - used to simplify compilation • makefile: a text file that contains recipes for building your executable file. • Carry out the build by typing make on the command line • For each target (object files, executable) you want to build, include: • dependencies: if these files change, you will need to re-build the target • command: command to carry out to build the target • make will only re-build a target if the dependencies have been modified. Format: target: [dependencies] <tab> <command>

makefile for coord # This is a comment. File name is makefile # The first target in file is the one that is built when you type "make" # To build a different target, type "make <target>" # There must be a tab at the beginning of the command lines. coord: coord. o helper. o gcc –o coord. o helper. o coord. o: coord. c gcc –c coord. c helper. o: helper. c gcc –c helper. c

Using the makefile • If I edit helper. c, and then type: % make gcc -c helper. c gcc -o coord. o helper. o -lm %. /coord Execute Enter the x value: 3 Enter the y value: 4 Distance from origin is 5. 00 These are the commands in makefile that had to be executed since helper. c changed. user input is underlined

Simple Example file 1. c file 2. c file 1. h #include "file 1. h" #include<stdio. h> #include "file 1. h" // Example include file int main() { // Call function in file 2. c file 2 Function(); } To compile and run: gcc –o file 1. c file 2. c. /file 1 void file 2 Function() { printf("Makefiles FTW"); } OR void file 2 Function(); gcc file 1. c file 2. c. /a. out Problem: If we have large. c files and change one, then we want to only recompile the one that changed, and link the. o files to get executable.

Exercise • So let's produce the object files from the. c files separately, and then link them, so that changes in one won't force us to recompile both: gcc –c file 1. c gcc –c file 2. c Write a makefile that contains 3 targets: file 1 (the executable), file 1. o and file 2. o. Note that a change in the file 1. h will affect file 1. o and file 2. o.
- Slides: 9