Compiling from source code

Slides:



Advertisements
Similar presentations
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.
Advertisements

Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
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.
Guide To UNIX Using Linux Third Edition
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
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.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:
Makefile Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile"
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Multiple file project management & Makefile
The make utility (original presentation courtesy of Alark Joshi)
Last week: We talked about: History of C Compiler for C programming
How to Program.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
User-Written Functions
Brief Intro to Make CST494/ Gannod.
CSC201: Computer Programming
A bit of C programming Lecture 3 Uli Raich.
Compilation and Debugging
Compilation and Debugging
Makefiles.
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Compiling from source code
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.
CS 201 A Whirlwind Tour of C Programming
Larger Projects, Object Files, Prototyping, Header Files, Make Files
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
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
Makefiles and the make utility
Data Structures and Programming Techniques
Govt. Polytechnic,Dhangar
Creating your first C program
CMPSC 60: Week 4 Discussion
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
CMSC 202 Additional Lecture – Makefiles
Kyle Fitzpatrick Konstantin Zak 11/29/2004
SCMP Software Development Spring 2018 James Skon
Palm Application Development & GUI Programming
Homework K&R chapter 4.
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
Compile and run c files.
SCMP Software Development Spring 2018 James Skon
SPL – PS1 Introduction to C++.
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

Compiling from source code Make a pgm using make*

make* Makefiles are a simple way to organize code compilation. A Simple Example start off with the following three files, 1) hellomake.c 2) hellofunc.c 3) hellomake.h

hellomake.c #include <hellomake.h> int main() { // call a function in another file myPrintHelloMake(); return(0); }

hellofunc.c #include <stdio.h> #include <hellomake.h> void myPrintHelloMake(void) { printf("Hello makefiles!\n"); return; }

hellomake.h /* example include file */ void myPrintHelloMake(void);

compiling Normally, you would compile this collection of code by executing the following command: $ gcc -o hellomake hellomake.c hellofunc.c -I. Create an o/p hellomake The -I. is Included so that gcc will look in the current directory (.) for the include file hellomake.h.

Imp. Of Makefile Without a makefile, the typical approach to the test/modify/debug cycle is to use the up arrow in a terminal to go back to your last compile command, so you don't have to type it each time, especially once you've added a few more .c files to the mix. Unfortunately, this approach to compilation has two downfalls.

Disadvantage of CLI manual 1) if you lose the compile command or switch computers you have to retype it from scratch, which is inefficient at best. 2) if you are only making changes to one .c file, recompiling all of them every time is also time- consuming and inefficient. So, it's time to see what we can do with a makefile.

Makefile / makefile - 1 The simplest makefile you could create would look something like: $vi Makefile hellomake: hellomake.c hellofunc.c // dependency list gcc -o hellomake hellomake.c hellofunc.c -I. $ make

Make* Hellomake: // rule make* with no arguments executes the first rule (hellomake:) in the file. VIMP: tab before the gcc command in the makefile knows that the rule hellomake: needs to be executed if any of those files change.

Makefile - 2 CC=gcc CFLAGS=-I. hellomake: hellomake.o hellofunc.o $(CC) -o hellomake hellomake.o hellofunc.o -I.

Constants we've defined some constants CC and CFLAGS. These are special constants that communicate to make* how we want to compile the files hellomake.c and hellofunc.c CC=gcc // must to use C compiler CFLAGS =-I. // list of flags to pass to the compilation command.

Explanation By putting the object files--hellomake.o and hellofunc.o--in the dependency list and in the rule, make* knows it must first compile the .c versions individually, and then build the executable hellomake. this form of makefile is sufficient for most small scale projects. there is one thing missing:

Makefile impr.. If you were to make a change to hellomake.h, for example, make* would not recompile the .c files, to fix this, we need to tell make that all .c files depend on certain .h files. We can do this by writing a simple rule and adding it to the makefile.

Makefile - 3 CC=gcc CFLAGS=-I. DEPS = hellomake.h %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) hellomake: hellomake.o hellofunc.o gcc -o hellomake hellomake.o hellofunc.o -I.

Explanation macro DEPS, which is the set of .h files on which the .c files depend. rule that applies to all files ending in the .o %.o: %.c $(DEPS) rule says that the .o file depends upon the .c version of the file and the .h files included in the DEPS macro.

Explanation $(CC) -c -o $@ $< $(CFLAGS) rule then says that to generate the .o file, make* needs to compile the .c file using the compiler defined in the CC macro. -c flag says to create/generate the object file, -o $@ says to put the output of the compilation in the file named on the left side of the (:) $< is the first item in the dependencies list, and the CFLAGS macro is defined as above.

Eg:1 For example, consider the following declaration: eg :- all: library.cpp main.cpp In this case: $@ evaluates to all $< evaluates to library.cpp $^ evaluates to library.cpp main.cpp

Eg 2 hello.o: hello.c hello.h gcc -c $< -o $@ Here, hello.o is the output file. This is what $@ exapnds to. The first dependency is hello.c. That's what $< expands to.

Makefile 4 CC=gcc CFLAGS=-I. DEPS = hellomake.h OBJ = hellomake.o hellofunc.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) hellomake: $(OBJ) gcc -o $@ $^ $(CFLAGS)

Explain $@ and $^, which are the left and right sides of the :, hellomake: $(OBJ) gcc -o $@ $^ $(CFLAGS) $@ - o/p must be hellomake (left side) $^ - take all obj files at the right side

IDIR =../include //.h files in an include directory CC=gcc CFLAGS=-I$(IDIR) // dot changes in to .../include dir ODIR=obj // object dir LDIR =../lib // library LIBS=-lm // library modules in os - /etc/ld.so.conf /cache // ldd command // ldconfig - v// _DEPS = hellomake.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) _OBJ = hellomake.o hellofunc.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) $(ODIR)/%.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) hellomake: $(OBJ) gcc -o $@ $^ $(CFLAGS) $(LIBS) .PHONY: clean clean: rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~