C Tutorial - Program Organization CS 537 - Introduction to Operating Systems.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CSE 303 Lecture 16 Multi-file (larger) programs
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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
CS201 – Makefile Tutorial. A Trivial Makefile # Trivial Makefile for puzzle1.c # Ray S. Babcock, CS201, MSU-Bozeman # 1/5/05 # puzzle1: puzzle1.c gcc.
Makefiles Tutorial adapted from and myMakeTutorial.txt.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 CS 201 Makefile Debzani Deb. 2 Remember this? 3 What is a Makefile? A Makefile is a collection of instructions that is used to compile your program.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
G++ and make Dan Wilson CS193 02/01/06. The g++ Compiler What happens when you call g++ to build your program? Phase 1, Compilation:.cpp files are compiled.
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.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Computer Science 210 Computer Organization Introduction to C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 3 Getting Started with C++
C Programming Tutorial – Part I CS Introduction to Operating Systems.
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.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
UNIT 13 Separate Compilation.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Separate Compilation make and makefiles
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
C Programming Separate Compilation Variable Lifetime and Scope make.
CMSC 1041 Introduction to C Creating your first C program.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
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.
An Introduction to the UNIX Make Utility Introduction: Although make can be used in conjunction with most programming languages all examples given here.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
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
Automating Builds with Makefiles
Computer Science 210 Computer Organization
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
CS1010 Programming Methodology
Computer Systems and Networks
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C
Creating your first C program
Writing Large Programs
Appendix F C Programming Environment on UNIX Systems
Compiler vs linker The compiler translates one .c file into a .o file
Introduction to C Topics Compilation Using the gcc Compiler
SPL – PS1 Introduction to C++.
Chapter 11 Introduction to Programming in C
Presentation transcript:

C Tutorial - Program Organization CS Introduction to Operating Systems

Simple Compilation To compile a program in Unix, use gcc Example: –prompt> gcc myProg.c The previous example compiles an executable file called a.out To give the executable a different name, use the -o option Another Example –prompt> gcc myProg.c -o myProg There are lots more command line options –see the man pages

File Organization Sophisticated programs have multiple files –program files (*.c) most of your code goes into these files –header files (*.h) mostly prototypes and structure definitions –object files compiled program files that are not linked have to be linked to become executable files –make file always named Makefile responsible for building the entire program

Program Files This is what gets compiled –includes all of the function definitions –before a function can be used, it must be declared prototypes these can either be declared in the program file or in a header file Always followed with a.c extension

Program File Dependencies Possible for a function defined in one program file to be used in another –the two (or more) program files must be linked The simple gcc compilation example shown earlier automatically links your program to some standard libraries –that is why you don’t need to define scanf in your code If you want to link to non-standard libraries, you must include them in the compilation command –prompt> gcc myProg.c common.c -o myProg this compiles both myProg.c and common.c and links them together

Example myProg.c #include #include “common.h” int main() { int *x, *y; x = (int*)malloc(sizeof(int)); y = (int*)malloc(sizeof(int)); getNumber(x); getNumber(y); print(“x”, *x, “y”, *y); swap(x, y); print(“x”, *x, “y”, *y); return 0; } common.c void getNumber(int* num) { printf(“Enter a number: “); scanf(“%d”, num); } void print(char* s1, int n1, char* s2, int n2) { printf(“%s: %d\n”, s1, n1); printf(“%s: %d\n”, s2, n2); } void swap(int* n1, int* n2) { int tmp = *n1; *n1 = *n2; *n2 = tmp; }

Header Files Often put all the prototypes for a specific program file into a separate file –call this the header file –always ends in a.h extension Helps the program files look cleaner Allows prototypes of one program file to be easily included in another program file –remember, can’t use a function until declaring it Example common.h void getNumber(int*, int*); void print(char*, int, char*, int); void swap(int*, int*);

Object Files Instead of compiling multiple files together, they can be compiled separately into object files –object files are not executable –multiple object files can then be linked to form an executable –use the -c option of gcc to create object files Example –prompt> gcc myProg.c -c –prompt> gcc common.c -c these two lines create myProg.o and common.o object files –prompt> gcc myProg.o common.o -o myProg links myProg.o and common.o to create the executable myProg

gcc The gcc program is both a compiler and a linker –it can compile *.c files into object files using the -c option –it can link multiple object files (*.o) and create an executable a.out by default some other name if the -o option is used –it can both compile and link if multiple *.c files are given on the same line

make Obviously, for very large programs (with many files), creating and linking all of the object files could be very tedious This process can be automated using make Makefile –this is a file that defines exactly how a program should be compiled and linked –it only compiles what needs compiling if a file has not been modified since it was last compiled, it won’t recompile it Whenever you run make, it finds the Makefile file –it then executes all the operations defined in Makefile

Makefile The basic make file consists of targets, rules, and commands –target: name of the object to be built –rules: which files, if modified, would require this target to be re-built –commands: how to rebuild a target if any of objects listed in the rules have been changed

Example Makefile myProg: myProg.o common.o gcc myProg.o common.o -o myProg myProg.o: myProg.c gcc myProg.c -c common.o: common.c common.h gcc common.c -c target rule command

More on Makefile To help simplify and generalize your make file, variables can be used –They are usually declared as all caps –They are proceeded by a dollar sign ($) and enclosed in parenthesis when used Comments can be included –the line must start with a pound sign (#) –entire line is ignored

Example Redone Makefile # compiler CC = gcc # linker LD = gcc # object files OBJS = myProg.o common.o myProg: $(OBJS) $(LD) $(OBJS) -o myProg myProg.o: myProg.c $(CC) myProg.c -c common.o: common.c common.h $(CC) common.c -c

More on make There are million and one options for make There are an equal number of uses Find a good book or web page to learn more –what you have seen here should get you started