introduction to programming 3d applications ce00056-1 lecture 6 compiler options and makefiles

24
Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Upload: dominick-daniel

Post on 13-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Introduction to Programming3D Applications

CE00056-1

Lecture 6

Compiler options and makefiles

Page 2: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compilation Factors Small programs single file

“Not so small” programs :

Many lines of code Multiple components More than one programmer

Page 3: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compilation Factors

Problems:

Long files are harder to manage

(for both programmers and machines) Every change requires long compilation Many programmers can not modify the

same file simultaneously Division to components is desired

Page 4: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compilation Factors Divide project to multiple files

Good division to components Minimum compilation when something is

changed Easy maintenance of project structure,

dependencies and creation

Compiler directives used to refer to external files Compilation can link several files together to create

single application from several files Can compile several source files, link to pre-compiled

object files or combination of these options

Page 5: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

C Preprocessor

Runs on a source code file before it gets to the compiler

Gets the source into compilable form Strips out comments Loads in header files Replaces constant names with their values Expands macros Can chop out unneeded sections of code

Page 6: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

C Preprocessor

Has its own very simple language Three main kinds of statements:

#include #define conditionals (#ifdef, #ifndef, #if, #else, #elif,

#endif)

Page 7: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header Files and #include

Syntax: #include <library name.h> Syntax: #include “header file.h” Angle brackets used for shared, standard

libraries (string.h, stdlib.h) Double-quotes used for header files you have

written yourself

Page 8: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header Files & #include

Syntax: #include “header file.h” #include works by essentially copying &

pasting all the contents of the header file into your program

So whatever you put into a header file will then be present in your actual source code file

Page 9: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header Files – Multiple use

Many source code files can all share the same header file

Useful for reusing code

Page 10: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header Files

To make program work, need to be able to use the functions in one source code file in the other source code files

Header files enable this Typically: one header file matched with each source

code file Header file contains the information that the *other*

source code files need to use the functions in the matching source code file

Page 11: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header File Contents cont.

If you put the prototype for a function into a header file, all the code in the files that #include that header file will be able to use that function.

Example: input.c has a function int getInt() defined prog.c has the main() function, which needs to

use getInt() put the prototype into input.h, and add the line

#include “input.h” to both input.c and prog.c!

Page 12: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Header File Contents

What is necessary for one function to call another function?

The second function must have been declared previously – eg, via a function prototype

Remember – function does not have to be defined until later – in fact, not until the linking step

Page 13: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Breaking Up Programs

Up to now: one source code file (.c suffix) per program

But can have multiple source code files and also header files for one program

Each source code file gets compiled into an object file (.o suffix) separately

Multiple object files can be linked together to make a single executable

Page 14: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Multiple Source Code Files

IMPORTANT: only ONE source code file per program can have a main() routine!

Other source code files just have other functions Typically, put all the functions that are related

thematically into one separate source code file eg, all functions that get user input might go into a file

called “input.c” Prototypes referring to the functions go in input.h

Page 15: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Example Program Structure

Page 16: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compiling multiple-file programs

Problem: if you try to do this: ‘cc –o input input.c’

Error! No main function defined Have to make the intermediate stage only:

‘cc –c -o input.o input.c’

Page 17: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compiling multiple-file programs

Problem: if you try to do this: ‘cc –o prog prog.c’

Error! No getInt() function defined Even though input.h is included in prog.c

Have to first compile intermediate stage: ‘cc –c -o prog.o prog.c’

Page 18: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compiling multiple-file programs

Next, need to compile together input.o and prog.o into one executable file: cc –o prog prog.o input.o

Produces one executable (named “prog”) out of the two separate object files

Can then run prog A better way to compile several programs in

this way is to provide a utility to compile and link in one go

Page 19: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Compiling multiple-file programs

Done in Unix by the Makefile mechanism A makefile is a file (script) containing :

Project structure (files, dependencies) Instructions for files creation

The make command reads a makefile, understands the project structure and makes up the executable

Note that the Makefile mechanism is not limited to C programs

Page 20: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

make operation Project dependencies tree is constructed Target of first rule should be created We go down the tree to see if there is a target

that should be recreated. This is the case when the target file is older than one of its dependencies

In this case we recreate the target file according to the action specified, on our way up the tree. Consequently, more files may need to be recreated

If something is changed, linking is usually necessary

Page 21: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

make operation - continued

make operation ensures minimum compilation, when the project structure is written properly

Do not write something like:

prog: main.c sum1.c sum2.ccc –o prog main.c sum1.c sum2.c

which requires compilation of all project when something is changed

Page 22: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Makefile example

Consider, previous example program contained in two separate source files. “prog.c” “input.c” Both contain header “input.h”

Require executable file to be named “prog” Next slide shows a simple makefile that could

be used.

Page 23: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Makefile example

# Make file for prog executableprog: input.o prog.o

cc –o prog input.o prog.oprog.o: prog.c input.h

cc –c –o prog.o prog.cinput.o: input.c input.h

cc –c –o input.o input.c

Comment line preceded with “#”

“Dependency line”

“Action line”

Dependency line + Actionline = “Rule”

Dependency line must start in column 1

Action line must begin with a tab character

Page 24: Introduction to Programming 3D Applications CE00056-1 Lecture 6 Compiler options and makefiles

Makefile example

# Make file for prog executableprog: input.o prog.o

cc –o prog input.o prog.oprog.o: prog.c input.h

cc –c prog.cinput.o: input.c input.h

cc –c input.c

Note –o ????.o not really required because it is default output