Download presentation
Presentation is loading. Please wait.
1
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references for now - only a little bit on namespaces and make files.)
2
Slide 12- 2 Overview 12.1 Separate Compilation 12.2 Namespaces - only a little of this 12.3 Simple Make Files
3
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12.1 Separate Compilation
4
Slide 12- 4 Separate Compilation C++ allows you to divide a program into parts Each part can be stored in a separate file Each part can be compiled separately All the files can be linked manually or with a make file.
5
Why are Separate Files Desirable? Studies have shown the human mind can only grasp a page or two of code at a time By busting a program into pieces, the program becomes more understandable. By busting a program into pieces, the program becomes more understandable. When we study classes, we will see a class definition can be stored separately from a program and this will allow you and others to use the class in multiple programs. When we study classes, we will see a class definition can be stored separately from a program and this will allow you and others to use the class in multiple programs. Teams of programmers can work on the same program, without having to share much information.
6
Example #include "helloworld.h" int main(){ hello_world(); return 0; } #include "helloworld.h" #include using namespace std; void hello_world(){ cout << "Hello World!\n"; } #ifndef _HELLOWORLD_H #define _HELLOWORLD_H #include void hello_world(); #endif file: main.cpp file: helloworld.cpp file: helloworld.h
7
A header file contains declarations and other basic information needed by a program to use a library of functions (or classes). It separates information that would have to be repeated for different parts of the program. They are named as any file is with a.h suffix. The.h suffix means this is a header file The.h suffix means this is a header file A program using a header file must include it using an include directive #include "XXX.h" Slide 12- 7 Naming The Header File - red box
8
Slide 12- 8 #include " " or ? To include a predefined header file use #include To include a predefined header file use #include tells the compiler to look where the system stores predefined header files tells the compiler to look where the system stores predefined header files To include a header file you wrote, use the double quotes #include "XXX.h" The double quotes usually cause the compiler to look in the current directory for the header file The double quotes usually cause the compiler to look in the current directory for the header file The directories to be searched can be changed by changing the environment variable $PATH, but we won't do this now. The directories to be searched can be changed by changing the environment variable $PATH, but we won't do this now.
9
Slide 12- 9 The Implementation File - magenta box Contains the definitions of the functions you wish to separate from the main function. Often has the same name as the header file but a different suffix Since our header file is named XXX.h, the implementation file often is named XXX.cpp But, as there may be more than one implementation file and more than one header file, different names can be used.
10
The Application file is the file that contains the program that is usually the main function. It is also called a driver file It is also called a driver file Must use an include directive to include the interface file: #include "XXX.h" Must use an include directive to include the interface file: #include "XXX.h" Slide 12- 10 The Application File - yellow box
11
Slide 12- 11 Running The Program Basic steps required to run a program: (Details vary from system to system!) Compile the implementation file Compile the implementation file Compile the application file Compile the application file Link the files to create an executable program using a utility called a linker Link the files to create an executable program using a utility called a linker Linking is often done automatically
12
In Our System All these steps, except for the run command, can be done either separately or as a single command. Method 1: g++ -g -o hello main.cpp helloworld.cpp hello is the executable file for a run Note: helloworld. h is NOT listed in the compile command. Note: List the -g and-o separately.
13
Slide 12- 13 Compile XXX.h ? The interface file is not compiled separately The preprocessor replaces any occurrence of #include "XXX.h" with the text of XXX.h before compiling The preprocessor replaces any occurrence of #include "XXX.h" with the text of XXX.h before compiling Both the implementation file and the application file contain #include "XXX.h" Both the implementation file and the application file contain #include "XXX.h" The text of XXX.h is seen by the compiler in each of these files There is no need to compile XXX.h separately
14
Slide 12- 14 Introduction to #ifndef To prevent multiple declarations, we can use these directives: #define _DTIME_H adds dtime.h to a list indicating dtime.h has been seen #define _DTIME_H adds dtime.h to a list indicating dtime.h has been seen #ifndef _DTIME_H checks to see if dtime.h has been defined #ifndef _DTIME_H checks to see if dtime.h has been defined #endif If dtime.h has been defined, skip to #endif #endif If dtime.h has been defined, skip to #endif
15
Recall for This Example #ifndef _HELLOWORLD_H #define _HELLOWORLD_H #include void hello_world(); #endif The first time a #include "helloworld.h" is found, helloworld.h is defined The first time a #include "helloworld.h" is found, helloworld.h is defined The next time a #include "helloworld.h" is found, all lines between #ifndef and #endif are skipped The next time a #include "helloworld.h" is found, all lines between #ifndef and #endif are skipped
16
HELLOWORLD_H is the normal convention for creating an identifier to use with ifndef It is the file name in all caps It is the file name in all caps Use ' _ ' instead of '. ' Use ' _ ' instead of '. ' Use '_' in the beginning Use '_' in the beginning You may use any other identifier, but will make your code more difficult to read Slide 12- 16 Why HELLOWORLD_H?
17
Slide 12- 17 Defining Libraries You can create your own libraries of functions If you have a collection of functions… If you have a collection of functions… Declare them in a header file with their comments Define them in an implementation file Use the library files just as you use predefined libraries.
18
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12.2 Namespaces
19
Slide 12- 19 Namespaces A namespace is a collection of name definitions, such as class definitions and variable declarations If a program uses classes and functions written by different programmers, it may be that the same name is used for different things If a program uses classes and functions written by different programmers, it may be that the same name is used for different things Namespaces help us deal with this problem Namespaces help us deal with this problem
20
Slide 12- 20 The Using Directive #include places names such as cin and cout in the std namespace The program does not know about names in the std namespace until you add using namespace std; (if you do not use the std namespace, you can define cin and cout to behave differently)
21
Slide 12- 21 The Global Namespace Code you write is in a namespace it is in the global namespace unless you specify a namespace it is in the global namespace unless you specify a namespace The global namespace does not require the using directive The global namespace does not require the using directive
22
Simple Make Files
23
What is make? Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use make to build and install the program.
24
Capabilities of Make Make enables the end user to build and install your package without knowing the details of how that is done -- because these details are recorded in the makefile that you supply. Make figures out automatically which files it needs to update, based on which source files have changed so compilations can be faster. It also automatically determines the proper order for updating files, in case one non-source file depends on another non-source file.
25
As a result, if you change a few source files and then run make, it does not need to recompile all of your program. It updates only those non-source files that depend directly or indirectly on the source files that you changed. It updates only those non-source files that depend directly or indirectly on the source files that you changed. Make is not limited to any particular language. For each non-source file in the program, the makefile specifies the shell commands to compute it. These shell commands can run a compiler to produce an object file, the linker to produce an executable, and other programs.
26
Make is not limited to building a package. You can also use make to control installing or deinstalling a package or anything else you want to do often enough to make it worth while learning how to write the makefile. Shorter makefiles can be written than the one shown here, but the form introduced initially is the simplest to write and understand.
27
Makefile with Multiple Files The make utility (program) is designed to organize and eventually save time when separately compiling programs especially for debugging purposes. make uses a file (usually named Makefile) containing goals as input Every goal has associated with it: A target file (or name of the goal) - file to be produced when this goal is executed A set of commands to execute A set of files this goal depends on (dependencies) - files to be used in the execution of the goals
28
Makefile with Multiple Files For separate compilation there are usually the following goals: The goal for linking (producing an executable) - depends on objects files that are used to create executable The goals for producing object files (compiling) - depends on source and header files
29
Example makefile with Multiple Files- Terminology # Makefile for helloworld program # executable # hello : main.o helloworld.o g++ -g -o hello main.o \ helloworld.o main.o : main.cpp helloworld.h g++ -g -c main.cpp helloworld.o : helloworld.cpp \ helloworld.h g++ -g -c helloworld.cpp clean : rm -f main.o helloworld.o hello name of goal
30
Example makefile with Multiple Files - Terminology # Makefile for helloworld program # executable # hello : main.o helloworld.o g++ -g -o hello main.o \ helloworld.o main.o : main.cpp helloworld.h g++ -g -c main.cpp helloworld.o : helloworld.cpp \ helloworld.h g++ -g -c helloworld.cpp clean : #no dependencies rm -f main.o helloworld.o hello dependencies in red
31
Example makefile with Multiple Files - Terminology # Makefile for helloworld program # executable # hello : main.o helloworld.o g++ -g -o hello main.o \ helloworld.o main.o : main.cpp helloworld.h g++ -g -c main.cpp helloworld.o : helloworld.cpp \ helloworld.h g++ -g -c helloworld.cpp clean : rm -f main.o helloworld.o hello command to execute in magenta
32
Example Makefile with Multiple Files - Cautions # Makefile for helloworld program # executable # hello : main.o helloworld.o g++ -g -o hello main.o \ helloworld.o main.o : main.cpp helloworld.h g++ -g -c main.cpp helloworld.o : helloworld.cpp \ helloworld.h g++ -g -c helloworld.cpp clean : rm -f main.o helloworld.o hello Must use TAB continuation of line marker
33
Assume that Makefile is the Name of the make File Any of these can be used to build hello assuming there is only one Makefile in the directory. make make Makefile make hello Many prefer to use make hello
34
What Happens? Issuing a "make hello" command will ask make to build hello. 1. If the dependent files and target file exist and the dependent files were not changed since the target file was created, then nothing is done except to tell the user that everything is up to date. 2. If the target file doesn't exist or it exists with a touch date before those on the dependent files, (assuming they all exist) then the command for the goal is executed.
35
What Happens? 3. Otherwise, for each dependent file, repeat steps 1, 2, and 3. 4. Execute the command to build the original target file. As the makefile executes, you will see what is happening with messages. Issuing a command of make clean make clean will just execute the remove command as there are no dependent files.
36
Some gdb Commands Useful for Multiple File Programs break filename.cpp: 2- sets a breakpoint on line in file filename.cpp break funcname - sets a breakpoint at the beginning of function funcname() regardless of the location of the file containing the function info breakpoints - lists all breakpoints set in the program
37
delete 6 - deletes a breakpoint by the number listed in info as 6 clear filename.cpp:5 or clear funcname - deletes the breakpoints by location in the program
38
Slide 12- 38 Chapter 12 -- End for Now
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.