CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
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.
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.
Makefiles Tutorial adapted from and myMakeTutorial.txt.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
Makefiles, and.h files, and.c files, and.o files, OH MY! For projects with more complexity. (Great.. Just what we needed)
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
UNIT 13 Separate Compilation.
Separating Definition & Implementation Headers and Comments.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
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.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Makefiles CSSE 332 Operating Systems
Lecture 3 Translation.
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Executable program (p1)
Brief Intro to Make CST494/ Gannod.
C Programming Hardik H. Maheta.
Compilation and Debugging
Compilation and Debugging
Chapter 13 - The Preprocessor
Makefiles Caryl Rahn.
Functions Separate Compilation
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
CS1010 Programming Methodology
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Pre-processor Directives
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
Separating Definition & Implementation
Comments, Prototypes, Headers & Multiple Source Files
Creating your first C program
C++ Compilation Model C++ is a compiled language
CSE 303 Concepts and Tools for Software Development
Writing Large Programs
Appendix F C Programming Environment on UNIX Systems
Compiler vs linker The compiler translates one .c file into a .o file
Executable program (p1)
Makefiles, GDB, Valgrind
SPL – PS1 Introduction to C++.
Presentation transcript:

CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles

CSE 251 Dr. Charles B. Owen Programming in C2 Lecture Outline What is gcc? – What are the different switches available? What is a Makefile? Exercise: – Take an existing file, split it into multiple separate programs, and write a Makefile to simplify compilation of the files

CSE 251 Dr. Charles B. Owen Programming in C3 gcc is not a single program gcc is a conglomeration of programs that work together to make an executable file We need to know what it does and how we can better control it. In particular, we would like to be able to make multiple, separate programs which can be combined into one executable

CSE 251 Dr. Charles B. Owen Programming in C4 What does gcc do? C source file #include #define NumIter 5 int main() { int i; for (i=1; i<=NumIter; i++) printf(“PI^%d is %f\n”, i, pow(M_PI, i)); } code.c

CSE 251 Dr. Charles B. Owen Programming in C5 What does gcc do? C source file preprocessor (# commands) Expanded C code Compiler Machine Code (.o file) Linker Executable Library Files

CSE 251 Dr. Charles B. Owen Programming in C6 Some gcc options Preprocessing ( gcc –E code.c > code.i ) – Removes preprocessor directives (commands that start with #) – Produces code.i Don’t use directly Compiling ( gcc –o code.o –c code.i ) – Converts source code to machine language with unresolved directives – Produces the code.o binary Linking ( gcc –lm –o code code.o ) – Creates machine language exectutable – Produces the code binary by linking with the math library (-lm)

CSE 251 Dr. Charles B. Owen Programming in C7 C Preprocessor, cpp C source file preprocessor (# commands) Expanded C code Processes commands that are preceded with # symbols and expands the code  #include  #define  #ifdef and #ifndef

CSE 251 Dr. Charles B. Owen Programming in C8 #include Include header files that contain declarations necessary for compiling code – the declarations provide the proper types, but not the actual definitions (the actual code)

CSE 251 Dr. Charles B. Owen Programming in C9 #include #include “functions.h” int main() { double x = 1; printf(“Sinc(x) = %f\n”, Sinc(x)); } data.c double Sinc(double x); functions.h # ifndef _STDIO_H # if !defined __need_FILE && !defined __need___FILE # define _STDIO_H 1 # include __BEGIN_DECLS … stdio.h gcc –E data.c > data.i data.i is like an “expanded” C code in which the #include command is “substituted” with text from the files

CSE 251 Dr. Charles B. Owen Programming in C10 Example void DisplayMe(int n); #include “display.h” int main() { DisplayMe(12); } display.h main.c A

CSE 251 Dr. Charles B. Owen Programming in C11 Header Files We’ve been using.h files to define things from the C libraries: stdio.h, stdlib.h, stdbool.h, etc. These are called Header Files. We’re going to create our own.h files to share information between.c files.

CSE 251 Dr. Charles B. Owen Programming in C12 #define #defines a preprocessor variable – every place the variable occurs, the definition will be substituted as code Syntax: #define var_name var_definition –Examples: #define DEBUG 1 #define PI

CSE 251 Dr. Charles B. Owen Programming in C13 #include #define PI #define square(x) (x*x) int main() { printf("Value of PI is %f\n", PI); printf("Square of 3.5 is %f\n", square(3.5)); } preproc.c # 1 “preproc.c" # 1 " " # 1 " “ … typedef unsigned int size_t; … int main() { printf("Value of PI is %f\n", ); printf("Square of 3.5 is %f\n", (3.5*3.5)); } gcc –E preproc.c > preproc.i preproc.i

CSE 251 Dr. Charles B. Owen Programming in C14 #ifdef, #ifndef, #if Surround code that might be included. Include for compilation when desired #ifdef DEBUG …. #endif #ifndef DEBUG … #endif

CSE 251 Dr. Charles B. Owen Programming in C15 Common use if(num == 1) { /* This is the only time we actually move a disk */ DisplayTower(tower); #if 0 printf("Press return"); fgets(cmd, sizeof(cmd), stdin); #endif MoveDisk(tower, fm, to); return; } This is a handy way to turn off a block of code without removing it. It often works better than trying to comment out the block, which may have comments already in it.

CSE 251 Dr. Charles B. Owen Programming in C16 Header Files #define NumDisks 6 #define NumPins 3 #include #include “hanoi.h” bool CheckDone(int tower[NumPins][NumDisks]); #include #include “hanoi.h” void DisplayTower(int tower[NumPins][NumDisks]) { … } hanoi.h: hanoi.c: display.c: I have two.c files in the same program. Both need to know NumDisks and NumPins. So, I put that information in a header file: hanoi.h

CSE 251 Dr. Charles B. Owen Programming in C17 Example #define NumDisks 6 #define NumPins 3 #include “hanoi.h” bool CheckDone(int tower[NumPins][NumDisks]); #if 0 bool DebugStuff(int x, int b[NumPins]); #endif hanoi.h: hanoi.c: B

CSE 251 Dr. Charles B. Owen Programming in C18 What would happen? #define NumDisks 6 #define NumPins 3 #include #include “solve.h” #include “hanoi.h” bool CheckDone(int tower[NumPins][NumDisks]); hanoi.h: hanoi.c: #include “hanoi.h” void Autosolve(int tower[NumPins][NumDisks]); solve.h: C

CSE 251 Dr. Charles B. Owen Programming in C19 Include Guards #ifndef HANOI_H #define HANOI_H #define NumDisks 6 #define NumPins 3 #endif #include #include “solve.h” #include “hanoi.h” bool CheckDone(int tower[NumPins][NumDisks]); hanoi.h: hanoi.c: #ifndef SOLVE_H #define SOLVE_H #include “hanoi.h” void Autosolve(int tower[NumPins][NumDisks]); #endif solve.h: Include Guards: Conditional compilation code that protects a section of code in a header from being compiled more than once.

CSE 251 Dr. Charles B. Owen Programming in C20 Compiling C source file preprocessor (# commands) Expanded C code Compiler Object Code (.o file) Compiler translates the C source code into object code – what the machine actually understands  Machine-specific Each line represents either a piece of data or a machine-level instruction  To create the assembly code: gcc -c preproc.c gcc –c code.c

CSE 251 Dr. Charles B. Owen Programming in C21 Linking Object file may not be directly executable – Missing some parts – Still has some names to be resolved The linker (ld) takes multiple object files (.o) and puts them together into one executable file – Resolves references to calls from one file to another Linking is important as it allows multiple, separate files to be brought together

CSE 251 Dr. Charles B. Owen Programming in C22 Linking C source file preprocessor gcc -E Expanded C code Machine Code (.o file) Linker Executable Library Files Compiler gcc -c gcc –lm code.o –o code

CSE 251 Dr. Charles B. Owen Programming in C23 Separate compilation For large, complex programs, it is a good idea to break your files into separate.c and.h files Can debug each separately, then reuse Can distribute pieces to other so they can incorporate into their code without changing their code (just link it in)

CSE 251 Dr. Charles B. Owen Programming in C24 Example (Separate Compilation) shapes.hshapes.cmainprog.c mainprog gcc -c shapes.c gcc –c mainprog.c gcc –lm mainprog.o shapes.o –o mainprog math.h

CSE 251 Dr. Charles B. Owen Programming in C25 Another way (Compile all at once) shapes.hshapes.cmainprog.c mainprog gcc –lm shapes.c mainprog.c –o mainprog math.h

CSE 251 Dr. Charles B. Owen Programming in C26 Controlling compilation/linking better We need a better way to control compilation – too much detail to type each time We can save time by only updating (compiling) the parts that need updating – if a.o file is OK (unchanged source), leave it. If a.c file is changed, the.o file needs to be regenerated then everything relinked

CSE 251 Dr. Charles B. Owen Programming in C27 Makefiles Suppose you have a program, which is made up of these 5 source files: main.c (include data.h, io.h) data.c (include data.h) data.h io.c (include io.h) io.h

CSE 251 Dr. Charles B. Owen Programming in C28 How to create the executable? data.c data.h main.cio.cio.h gcc -c data.c  data.o gcc -c io.c  io.o gcc -c main.c  main.o gcc main.o io.o data.o –o executable

CSE 251 Dr. Charles B. Owen Programming in C29 How to create the executable? data.c data.h main.cio.cio.h gcc -c data.c gcc -c io.c gcc -c main.c gcc main.o io.o data.o –o executable What if you modify data.h? Do you need to re-run gcc -c on io.c and main.c?

CSE 251 Dr. Charles B. Owen Programming in C30 Dependencies data.c data.h main.cio.cio.h data.omain.oio.o executable

CSE 251 Dr. Charles B. Owen Programming in C31 What is a Makefile? A Makefile is used with the make utility to determine which portions of a program to compile. It is basically a script that guides the make utility to choose the appropriate program files that are to be compiled and linked together

CSE 251 Dr. Charles B. Owen Programming in C32 If data.h is modified data.c data.h main.cio.cio.h data.omain.oio.o executable No need to re-create data.o and main.o

CSE 251 Dr. Charles B. Owen Programming in C33 If io.c is modified data.c data.h main.cio.cio.h data.omain.oio.o executable No need to re-create data.o and main.o

CSE 251 Dr. Charles B. Owen Programming in C34 #ifndef HANOI_H #define HANOI_H #define NumDisks 6 #define NumPins 3 #endif #include #include “solve.h” #include “hanoi.h” bool CheckDone(int tower[NumPins][NumDisks]); hanoi.h:hanoi.c: #ifndef SOLVE_H #define SOLVE_H #include “hanoi.h” void Autosolve(int tower[NumPins][NumDisks]); #endif solve.h: #include #include “hanoi.h” bool DisplayTower(int tower[NumPins][NumDisks]); display.c: D

CSE 251 Dr. Charles B. Owen Programming in C35 What is in a Makefile? file: dependency1 dependency2 command name in column1 TAB character spaces not commas

CSE 251 Dr. Charles B. Owen Programming in C36 What it means The first line indicates what a file depends on. That is, if any of the dependencies change, then that file must be updated – What updating means is defined in the command listed below the dependency Don’t forget the TAB character to begin the second line

CSE 251 Dr. Charles B. Owen Programming in C37 Dependency graph data.c data.h main.cio.cio.h data.omain.oio.o Executable (prog)

CSE 251 Dr. Charles B. Owen Programming in C38 Creating a Makefile data.c data.h main.cio.cio.h data.omain.oio.o Executable (prog) prog: data.o: main.o: io.o: Makefile: List the object (*.o) and executable files that make up the program

CSE 251 Dr. Charles B. Owen Programming in C39 Creating a Makefile data.c data.h main.cio.cio.h data.omain.oio.o Executable (prog) Makefile: First line specifies the dependencies for each object and executable files prog: data.o main.o io.o gcc –o prog data.o main.o io.o data.o: data.h data.c gcc –c data.c main.o: data.h io.h main.c gcc –c main.c io.o: io.h io.c gcc –c io.c

CSE 251 Dr. Charles B. Owen Programming in C40 Creating a Makefile data.c data.h main.cio.cio.h data.o main.oio.o Executable (prog) prog: data.o main.o io.o gcc –o prog data.o main.o io.o data.o: data.h data.c gcc –c data.c main.o: data.h io.h main.c gcc –c main.c io.o: io.h io.c gcc –c io.c Makefile: Second line contains the command to execute if any of the dependence files are modified

CSE 251 Dr. Charles B. Owen Programming in C41 Additional makefile “targets” clean: rm -rf *.o example22 Command “make clean” calls the clean command In this case, clears the.o files and the executable

CSE 251 Dr. Charles B. Owen Programming in C42 Usage make – To create the executable make linkedList.o – do what has to be done to make that.o file, then stop make clean – run the clean command 1