Download presentation
Presentation is loading. Please wait.
Published byMaximilian Brooks Modified over 9 years ago
1
Introduction to C Programming CE00312-1 Lecture 7 Compiler options and makefiles
2
Compilation Factors Small programs single file “Not so small” programs : Many lines of code Multiple components More than one programmer
3
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
4
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
5
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
6
C Preprocessor Has its own very simple language Three main kinds of statements: #include #define conditionals (#ifdef, #ifndef, #if, #else, #elif, #endif)
7
Header Files and #include Syntax: #include 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
8
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
9
Header Files – Multiple use Many source code files can all share the same header file Useful for reusing code
10
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
11
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!
12
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
13
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
14
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
15
Example Program Structure
16
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’
17
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’
18
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
19
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
20
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
21
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.c cc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed
22
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.
23
Makefile example # Make file for prog executable prog: input.o prog.o cc –o prog input.o prog.o prog.o: prog.c input.h cc –c –o prog.o prog.c input.o: input.c input.h cc –c –o input.o input.c Comment line preceded with “#” “Dependency line” “Action line” Dependency line + Action line = “Rule” Dependency line must start in column 1 Action line must begin with a tab character
24
Makefile example # Make file for prog executable prog: input.o prog.o cc –o prog input.o prog.o prog.o: prog.c input.h cc –c prog.c input.o: input.c input.h cc –c input.c Note –o ????.o not really required because it is default output
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.