1 / 43

Program Management

Program Management. Program Management. Program Building Code Organization Program Distribution Methods. main.c. main.o. a.out. Program Building. Basic Steps in building a program : compile Link Source code file Object Code file Executable ritu > ls sqrt.c

Télécharger la présentation

Program Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Management Ritu Chaturvedi 60-321

  2. Program Management Program Building Code Organization Program Distribution Methods Ritu Chaturvedi 60-321

  3. main.c main.o a.out Program Building Basic Steps in building a program : compile Link Source code file Object Code file Executable ritu> ls sqrt.c ritu>gcc –c sqrt.c ritu>ls sqrt.csqrt.o ritu> Ritu Chaturvedi 60-321

  4. Linking • 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 Ritu Chaturvedi 60-321

  5. Linking ritu>gcc –c sqrt.c ritu>gcc –c main.c ritu>ls main.cmain.osqrt.csqrt.o ritu>gccsqrt.omain.o ritu>ls a.outmain.cmain.osqrt.csqrt.o Ritu Chaturvedi 60-321

  6. Linking • Linking primarily serves to bring together object code files into an executable. • It can also bring in object code from library files . compile Link Source code file Object Code file Executable Link main.c main.o a.out libc.a Ritu Chaturvedi 60-321

  7. main.c a.out libc.a Linking compile Link Source code file Object Code file Executable 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. • gccmain.osqrt.o -lc main.o Ritu Chaturvedi 60-321

  8. Make overview • 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 Ritu Chaturvedi 60-321

  9. Make overview • 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 Ritu Chaturvedi 60-321

  10. Makefile • 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] Ritu Chaturvedi 60-321

  11. Makefile • 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 …) Ritu Chaturvedi 60-321

  12. Makefile Rule Target : The file to create Dependency List : The files that are required to create the object file. Foobar.o: Foobar.cFoobar.h gcc -Wall -c Foobar.c Tab : Used to signal what follows as an action Action: What needs to be done to create the target Ritu Chaturvedi 60-321

  13. An example makefile 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 Ritu 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. Ritu 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. Ritu Chaturvedi 60-321

  17. The final tree : main main1.o reverse.o Order of make rules main1.c reverse.h reverse.h reverse.c Ritu Chaturvedi 60-321

  18. Order of make rules main1 7 main1.o 5 reverse.o 6 main1.c 1 reverse.h 2 reverse.c 3 reverse.h 4 • 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. Ritu 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 : 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 Ritu Chaturvedi 60-321

  20. Executing a makefile 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 Ritu Chaturvedi 60-321

  21. Comments in a makefile The simplest makefile statement is a comment, which is indicated by the commentcharacter “#”. 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 Ritu Chaturvedi 60-321

  22. Example :Write a makefile that can be used to manage the dact program. Dact Avail.o Resp.o Cpu.o Main.o Main1.c Dact.h Avail.c Resp.c Resp.h Cpu.c Ritu Chaturvedi 60-321

  23. Make variables (macros) • 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.c 123=variable names may start with a number OBJ = main.o C = this variable is going to be continued \ TESTVAR = this is a test on the next line Ritu Chaturvedi 60-321

  24. Make variables (macros) • 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-321

  25. Make variables (macros) • 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-321

  26. Make variables (macros) (Example from slide 14 repeated ) Ritu Chaturvedi 60-321

  27. Cleaning up ! One of the entries in the makefile is clean : rm test.o > make clean Ritu Chaturvedi 60-321

  28. Further study Visit the website given below for more information on make and makefile: http://www.gnu.org/software/make/manual/make.html 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 Ritu 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. • 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 havethe 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 Ritu Chaturvedi 60-321

  31. Scons – first build • In the $HOME/bin folder, create a program called 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 ! Ritu Chaturvedi 60-321

  32. Scons – first build • 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. Ritu Chaturvedi 60-321

  33. Other build methods • Another is the Object builder method, which tells SCons to build an object file from the specified source file: • Object('hello.c') Ritu Chaturvedi 60-321

  34. Cleaning Up ! • Cleaning Up After a Build Scons –c • Less verbose option ! Scons -Q Ritu Chaturvedi 60-321

  35. Specifying the Name of the Target (Output) File • 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) Ritu Chaturvedi 60-321

  36. Keyword Arguments Program (target=‘program’, source = ‘hello.c’) OR Program (source = ‘hello.c’, target=‘program’) Ritu Chaturvedi 60-321

  37. Compiling Multiple Source Files • 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 1st course file => Program( ‘my_exec’, [‘prog.c’, ‘file1.c’, ‘file2.c’]) Ritu Chaturvedi 60-321

  38. Compiling Multiple Programs In the Sconstruct file , call the Program method multiple times => Program(‘hello.c’) Program(‘program’, [‘file1.c, file2.c]) Ritu Chaturvedi 60-321

  39. Compiling Multiple Programs Ritu Chaturvedi 60-321 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. 39

  40. Compiling Multiple Programs Ritu Chaturvedi 60-321 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) 40

  41. Compiling Multiple Programs Ritu Chaturvedi 60-321 Absolute Path Names can be used : env = Environment() env.Program('prog', ['main.c', ‘/global/fac3/rituch/321/reverse.c’]) 41

  42. Compiling Multiple Programs Ritu Chaturvedi 60-321 • 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) 42

  43. Building and Linking with Libraries (to be done after the chapter on libraries) Ritu Chaturvedi 60-321

More Related