Download presentation
Presentation is loading. Please wait.
Published byElisabeth Allen Modified over 8 years ago
1
1 Ritu Chaturvedi 60-321
2
- Program Building - Code Organization - Program Distribution Methods 2 Ritu Chaturvedi 60-321
3
Basic Steps in building a program : compileLink Source code fileObject Code fileExecutable ritu> ls sqrt.c ritu>gcc –c sqrt.c ritu>ls sqrt.c sqrt.o ritu> main.cmain.oa.out 3Ritu Chaturvedi 60-321
4
- Linking is the process of bringing together multiple pieces of object code and arranging them into an executable. - Object code can come from multiple source code files, each compiled into its own object code file. - When compiling (single files), unless otherwise instructed, gcc will automatically proceed to linking, and then remove any object code files it creates #include int main(int argc, char *argv[]) { intx,s; printf("Enter any integer: "); scanf("%d",&x); s=SquareRoot(x); if (s != -1) printf("The square root of %d is %d\n",x,s); } #include int SquareRoot(int n) { if (n == 4) return(2); else { printf("I do not know how to compute the square root of %d\n",n); return(-1); } 4Ritu Chaturvedi 60-321
5
ritu>gcc –c sqrt.c ritu>gcc –c main.c ritu>ls main.c main.o sqrt.c sqrt.o ritu>gcc sqrt.o main.o ritu>ls a.out main.c main.o sqrt.c sqrt.o #include int main(int argc, char *argv[]) { intx,s; printf("Enter any integer: "); scanf("%d",&x); s=SquareRoot(x); if (s != -1) printf("The square root of %d is %d\n",x,s); } #include int SquareRoot(int n) { if (n == 4) return(2); else { printf("I do not know how to compute the square root of %d\n",n); return(-1); } 5Ritu Chaturvedi60-321
6
- Linking primarily serves to bring together object code files into an executable. - It can also bring in object code from library files. compileLink Source code fileObject Code fileExecutable Link main.cmain.oa.out libc.a 6Ritu Chaturvedi 60-321
7
compileLink Source code fileObject Code fileExecutable Link - The most commonly used library in C is libc.a, the standard C library. It contains the object code for functions like printf(), scanf() etc. - The object code for these functions is permanently stored in a library file, so they do not need to be recompiled every time. - gcc main.o sqrt.o -lc main.o main.ca.out libc.a 7Ritu Chaturvedi 60-321
8
- Make is a utility that automates the compilation of programs whose files are dependent on each other. - A program typically consists of many files and a programmer usually works on only a few of them at a time -Typically, a vast majority of files remain unchanged and thus do not need to be recompiled 8Ritu Chaturvedi 60-321
9
- Make will only recompile the files that need to be updated (files that depend on updated files) => this is much faster than recompiling the whole program each time. - make looks at the timestamps of source files (*.c, *.h) that are required to generate an object file (*.o) - If a source is newer then the object file, the object file needs to be recompiled - Likewise if an object file is newer than the executable, it needs to be re-linked 9Ritu Chaturvedi 60-321
10
- For a program that is built from a large number of files, a number of compile commands may need to be performed. - Some of these commands may involve multiple filenames and command line options. - A makefile is a tool that allows a programmer to organize the compile commands and intermediate files and to execute rebuilds with less manual typing. - make is a utility that allows you to create a makefile that contains a list of all interdependencies for each executable file. - make [-f makefile] 10Ritu Chaturvedi 60-321
11
- A makefile is a list of rules of what files are required to generate an object or an executable file - File typically called makefile or Makefile, though it can be any name - Each rule consists of 4 parts: – Target: the name of the object or executable to create – Dependency List: the list of files that the target is dependent upon – TAB: used to set off an action – Action(s): a list of actions to take in order to create the target (i.e. gcc …) 11Ritu Chaturvedi 60-321
12
Foobar.o: Foobar.c Foobar.h gcc -Wall -c Foobar.c Target : The file to create Dependency List : The files that are required to create the object file. Tab : Used to signal what follows as an action Action: What needs to be done to create the target 12Ritu Chaturvedi 60-321
13
/* MAIN1.C */ #include #include "reverse.h" main () { char str [100]; reverse ("cat", str); printf ("reverse (\"cat\") = %s\n", str); reverse ("noon", str); printf ("reverse (\"noon\") = %s\n", str); } /* REVERSE.C */ #include #include "reverse.h" reverse (before, after) char *before; char *after; { int i; int j; int len; len = strlen (before); for (j = len - 1, i = 0; j >= 0; j--, i++) after[i] = before[j]; after[len] = NULL; } /* REVERSE.H */ reverse (); 13 Ritu Chaturvedi 60-321
14
An Example makefile >pico makefile main1 : main1.o reverse.o gcc main1.o reverse.o –o main1 main1.o : main1.c reverse.h gcc –c main1.c reverse.o :reverse.c reverse.h gcc –c reverse.c 14Ritu Chaturvedi 60-321
15
Makefile Rule main1.o : main1.c reverse.h gcc –c main1.c This rule says two things: - How to decide whether main1.o is out of date: it is out of date if it does not exist, or if either main1.c or reverse.h is more recent than it. - How to update the file main1.o: by running gcc as stated. The command does not explicitly mention reverse.h, but we presume that main1.c includes it, and that that is why reverse.h was added to the prerequisites. 15Ritu Chaturvedi 60-321
16
Order of make rules - the make utility creates a “tree” of interdependencies by initially examining the first rule. - Initial make dependency tree main1 main1.o reverse.o - the make utility then visits each rule associated with each file in the dependency list and performs the same actions. 16Ritu Chaturvedi 60-321
17
The final tree : main main1.o reverse.o Order of make rules main1.creverse.h reverse.c reverse.h 17Ritu Chaturvedi 60-321
18
Order of make rules main1 7 main1.o 5 reverse.h 4 reverse.c 3 reverse.h 2 main1.c 1 reverse.o 6 -The make utiltity works up the tree from the bottom leaf nodes to the root node, looking to see if the last modification time(LMT) of each child node is more recent that the LMT of its immediate parent node. -For each such case, the associated parent’s rule is executed. 18Ritu Chaturvedi 60-321
19
Executing a makefile If the make file that is shown in slide 14 is named as main1,the command to run the makefile is : 19Ritu Chaturvedi 60-321 luna:~/321>make -f makefile gcc -c main1.c gcc -c reverse.c gcc main1.o reverse.o -o main1 luna:~/321>main1 reverse done reverse ("cat") = tac reverse done reverse ("noon") = noon luna:~/321>ls
20
Executing a makefile 20Ritu Chaturvedi 60-321 If the command make is given more than once and the LMT has not changed, then the dependent files are not recompiled. luna:~/321>make -f makefile make: `main1' is up to date. luna:~/321>touch reverse.h# Change last modification to now luna:~/321>make -f makefile# Since main1.c and reverse.c gcc -c main1.c# are both dependent on reverse.h gcc -c reverse.c# they both get recompiled gcc main1.o reverse.o -o main1
21
Comments in a makefile The simplest makefile statement is a comment, which is indicated by the comment character “#”. All text from the comment character to the end of the line is ignored. Here is a large comment as might appear in a makefile to describe its contents: # My first Makefile 21Ritu Chaturvedi 60-321
22
Example : Write a makefile that can be used to manage the dact program. 22Ritu Chaturvedi 60-321 Main.o Dact Main1.c Cpu.oResp.oAvail.o Resp.hResp.c Avail.c Dact.hCpu.c
23
Make allows you to assign strings to variables and later recall their contents. Make variables are also known as macros A make variable is assigned a value by using the variable name on the LHS of an equal sign: Variable=value Examples of variables : FILES= abc.c def.c ghi.c123=variable names may start with a number OBJ = main.oC = this variable is going to be continued \ TESTVAR = this is a teston the next line Ritu Chaturvedi 60-32123
24
To access make variables, enclose them in parenthesis and precede them with a dollar sign. This causes the contents of the variable to be substituted at that point. For example : > cat makefile TESTVAR = this is a test test : test.c echo $(TESTVAR) cc –o test test.c Ritu Chaturvedi 60-32124
25
Make has certain predefined variables For C programmers, the variables that come into play are CC and CFLAGS CC is normally set to cc (command to compile) CFLAGS is normally set to –o Note that you cannot use a variable on both the left and right hand side of an = sign => OBJS = $(OBJS) test.o is illegal ! Ritu Chaturvedi 60-32125
26
Make variables (macros) (Example from slide 14 repeated ) main1 : main1.o reverse.o gcc main1.o reverse.o –o main1 main1.o : main1.c reverse.h gcc –c main1.c reverse.o :reverse.c reverse.h gcc –c reverse.c CC = gcc CFLAGS = -O -o OBJECTS = main1.o reverse.o TESTVAR = this is a test main1: $(OBJECTS) echo $(TESTVAR) $(CC) $(OBJECTS) $(CFLAGS) main1 main1.o : main1.c reverse.h $(CC) –c main1.c reverse.o :reverse.c reverse.h $(CC) –c reverse.c 26Ritu Chaturvedi 60-321
27
Cleaning up ! One of the entries in the makefile is clean : rm test.o > make clean 27Ritu Chaturvedi 60-321
28
Visit the website given below for more information on make and makefile: http://www.gnu.org/software/make/manual/make.html 28 Ritu Chaturvedi 60-321
29
SCONS Open Source software SCons is a software construction tool, a make tool--that is, a software utility for building software (or other files) and keeping built software up-to-date whenever the underlying input files change. The configuration files are actually scripts, written in the Python programming language. SCons still has a learning curve, of course, because you have to know what functions to call to set up your build properly, but the underlying syntax used should be familiar to anyone who has ever looked at a Python script 29Ritu Chaturvedi 60-321
30
SCONS We have python installed on the sol server download either the scons-1.2.0.tar.gz or scons-1.2.0.zip, which are available from the SCons download page at http://www.scons.org/download.html.http://www.scons.org/download.html Unpack the archive you downloaded, using a utility like tar on UNIX, This will create a directory called scons-1.2.0, usually in your local directory. Then change your working directory to that directory and install SCons by executing the following commands: > cd scons-1.2.0 Since we don’t have the admin privileges to install SCons in a system location, simply use the -- prefix= option to install it in a location of your choosing. For example, to install SCons in appropriate locations relative to the user's $HOME directory, the scons script in $HOME/bin and the build engine in $HOME/lib/scons, simply type: > python setup.py install --prefix=$HOME 30Ritu Chaturvedi 60-321
31
In the $HOME/bin folder, create a program called hello.c hello.c Enter the following into a file named SConstruct Program('hello.c') This minimal configuration file gives SCons two pieces of information: ◦ what you want to build (an executable program), and ◦ the input file from which you want it built (the hello.c file). ◦ Program is a builder_method, a Python call that tells SCons that you want to build an executable program. Now run the scons command ! 31Ritu Chaturvedi 60-321
32
Now run the scons command ! luna:~/bin>scons scons: Reading SConscript files... scons: done reading SConscript files. scons: Building targets... cc -o hello.o -c hello.c cc -o hello hello.o scons: done building targets. Also, when you ls now, you see 2 files : hello.o and hello in your folder. 32Ritu Chaturvedi 60-321
33
Another is the Object builder method, which tells SCons to build an object file from the specified source file:Object Object('hello.c') 33Ritu Chaturvedi 60-321
34
Cleaning Up After a Build Scons –c Less verbose option ! Scons -Q 34Ritu Chaturvedi 60-321
35
Program(‘hello.c’) builds an executable file called hello. If you wish to build an executable with a name different than the base of the source file name => Program(‘new_hello’, ‘hello.c’) (Imp: Target must appear first unless keyword arguments are used) 35Ritu Chaturvedi 60-321
36
Program (target=‘program’, source = ‘hello.c’) OR Program (source = ‘hello.c’, target=‘program’) 36Ritu Chaturvedi 60-321
37
To compile multiple source files => Program([‘prog.c’, ‘file1.c’, ‘file2.c’]) The above will build an executable called prog If you wish to build an executable with a name different from the base of the 1 st course file => Program( ‘my_exec’, [‘prog.c’, ‘file1.c’, ‘file2.c’]) 37Ritu Chaturvedi 60-321
38
In the Sconstruct file, call the Program method multiple times => Program(‘hello.c’) Program(‘program’, [‘file1.c, file2.c]) 38Ritu Chaturvedi 60-321
39
If the Sconstruct file consists of : Program('main1_from_scons', ['main1.c', 'reverse.c']) And we run scons, the result is => luna:~/bin>scons scons: Reading SConscript files... Calling program ('main1.c') scons: done reading SConscript files. scons: Building targets... cc -o main1_from_scons main1.o reverse.o scons: done building targets. Compiling Multiple Programs 39Ritu Chaturvedi 60-321
40
Use of Split : Program('main1_from_scons', ['main1.c', 'reverse.c']) Can be written (for convenience) as : Program('main1_from_scons', Spilt('main1.c reverse.c')) OR as : src_files = Split ('main1.c reverse.c') Program('main1_from_scons', src_files) Compiling Multiple Programs 40Ritu Chaturvedi 60-321
41
Absolute Path Names can be used : env = Environment() env.Program('prog', ['main.c', ‘/global/fac3/rituch/321/reverse.c’]) Compiling Multiple Programs 41Ritu Chaturvedi 60-321
42
-If your header file is in the same folder as the source, you do not need to specify anything - scons finds the header files. -If your header file is not in same other folder as source e.g. header file is in /global/fac/ritu/321 and source programs are in /global/fac/ritu/bin, you will have to set a path as shown below: env= Environment(CPPPATH = ['/global/fac3/rituch/321']) src_files=Split('main1.c reverse.c') env.Program ('output', src_files) Compiling Multiple Programs 42Ritu Chaturvedi 60-321
43
(to be done after the chapter on libraries) 43Ritu Chaturvedi 60-321
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.