Introduction to C Programming CE00312-1 Lecture 7 Compiler options and makefiles.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
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.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 The Makefile Utility ABC – Chapter 11,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Creating your first C++ program
Programming With C.
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.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Computer Programming I Hour 2 - Writing Your First C Program.
C Hints and Tips The preprocessor and other fun toys.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
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.
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
The make utility (original presentation courtesy of Alark Joshi)
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
INC 161 , CPE 100 Computer Programming
Compilation and Debugging
Compilation and Debugging
Chapter 13 - The Preprocessor
Makefiles Caryl Rahn.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Pre-processor Directives
Introduction to Programming
Separate Compilation.
Chapter 11 Introduction to Programming in C
C Preprocessor(CPP).
Creating your first C program
Writing Large Programs
C Preprocessor Seema Chandak.
C Programming Language
What Is? function predefined, programmer-defined
Compiler vs linker The compiler translates one .c file into a .o file
Introduction to C Topics Compilation Using the gcc Compiler
Makefiles, GDB, Valgrind
SPL – PS1 Introduction to C++.
Presentation transcript:

Introduction to C Programming CE Lecture 7 Compiler options and makefiles

Compilation Factors Small programs single file “Not so small” programs : Many lines of code Multiple components More than one programmer

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

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

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

C Preprocessor Has its own very simple language Three main kinds of statements: #include #define conditionals (#ifdef, #ifndef, #if, #else, #elif, #endif)

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

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

Header Files – Multiple use Many source code files can all share the same header file Useful for reusing code

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

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!

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

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

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

Example Program Structure

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’

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’

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

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

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

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

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.

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

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