Agenda Make Utility Command Line Arguments in Unix Individual Assignment – 8-Puzzle
Separate Compilation using ‘make’ Two processes occur when we compile a program: 1) Source code is compiled into an object file. 2) Object file is linked to form a load module or executable file.
Compilation g++ -Wall -c myfile.cc 2) Translates source code into machine readable code. 3) References to externally defined functions and/or variables are incomplete.
Linking g++ -o myfile myfile.o Resolves external references left hanging at compilation time. Creates a monolithic executable file.
Separate Compilation This is the process of – 1) Compiling a set of object files, individually. (multiple steps) 2) Then, linking those object files into an executable file. (one step)
Need of Separate Compilation When working on a substantial programming project, we want to use separate compilation because: 1) Entire program not compiled after only one small change, i.e. faster. 2) Team members can compile parts of a program (obj files) to be collected and linked later. 3) The "make" program will "make" life easy!!! (once its set up)
Sample Makefile CC = g++ BFILES = insertion.o insertionf.o insertion : $(BFILES) $(CC) -o insertion $(BFILES) insertion.o: insertion.cpp insertion.h $(CC) -c insertion.cpp
Sample Makefile […contd] insertionf.o: insertionf.cpp insertion.h $(CC) -c insertionf.cpp clean: rm *.o rm insertion
Terms and Definitions CC = g++ Macro Definition BFILES = insertion.o insertionf.o Macro Definition insertion : $(BFILES) Macro Expansion $(CC) -o insertion $(BFILES) Macro Definition insertion.o: insertion.cpp insertion.h $(CC) -c insertion.cpp Macro Definition
Command Line Arguments in Unix/C++ Say you have a program to calculate the average of input values. (avgPrgm) It looks better to invoke the program as follows $ avgPrgm 5 56 7 3 17 …… rather than asking the user to first enter the number of values that he/she wants to find the average of.
Use of argv and argc A program that does: A program that does not allow the use of command line arguments: Main() { //Statements } A program that does: Main(int argc, char* argv[])
Use of argv and argc [….Contd] argc: a count of number of arguments provided on the command line. argv[]: a vector (array) of pointers to char (strings) representing the command line elements.
Use of argv and argc [Example] Suppose main is the main program in command.cc and it is compiled and run with the following: 38%g++ command.cc –o command 39%command foo bar baz
Eaxmple […contd] …..then when main begins argv and argc are as follows: 4 argc argv f o \0 c m a n d b r
An Example Using argv and argc //Demonstrate the use of argv and argc # include <iostream.h> main(int argc, char *argv[]) { int i; cout<<“The number of elements on the command line:” <<argc<<endl; cout<<“The name of the program:<<argv[0]<<endl; for (i=1; i<argc; i++) cout<<“Command line argument number”<<i<<“:”<<arg[i]<<endl; }
Eaxmple […contd] 38% g++ cmdline.cc –o cmdline 39% cmdline foo bar baz The number of elements on command line:4 The name of the program:cmdline Command line argument number1:foo Command line argument number2:bar Command line argument number3:baz 40%
An Equivalent Usage Since: 1) argv[] is of type char* 2) and since arrays in C/C++ are pointers, then: argv is of type char ** Therefore, void main::process(int argc, char *argv[]) is equivalent to void main::process(int argc, char **argv) Note that following is an optimization: void main::process(int argc, const char **argv)