9.3 Compilation and Linking Process

The diagram in Fig. 9.4 shows the compilation and linking process that results in an executable Time application that can be used by instructors. Often a class’s interface and implementation will be created and compiled by one programmer and used by a separate programmer who implements the client code that uses the class. So, the diagram shows what’s required by both the class-implementation programmer and the client-code programmer. The dashed lines in the diagram show the pieces required by the class-implementation programmer, the client-code programmer and the Time application user, respectively. [Note: Figure 9.4 is not a UML diagram.]

A class-implementation programmer responsible for creating a reusable Time class creates the header Time.h and the source-code file Time.cpp that #includes the header, then compiles the source-code file to create Time’s object code. To hide the class’s member-function implementation details, the class-implementation programmer would provide the client-code programmer with the header Time.h (which specifies the class’s interface and data members) and the Time object code (i.e., the machine-code instructions that represent Time’s member functions). The client-code programmer is not given Time.cpp, so the client remains unaware of how Time’s member functions are implemented.

The client-code programmer needs to know only Time’s interface to use the class and must be able to link its object code. Since the interface of the class is part of the class definition in the Time.h header, the client-code programmer must have access to this file and must #include it in the client’s source-code file. When the client code is compiled, the compiler uses the class definition in Time.h to ensure that the main function creates and manipulates objects of class Time correctly.

To create the executable Time application, the last step is to link

  1. the object code for the main function (i.e., the client code),

  2. the object code for class Time’s member-function implementations, and

  3. the C++ Standard Library object code for the C++ classes (e.g., string) used by the class-implementation programmer and the client-code programmer.

The linker’s output is the executable Time application that users can execute to create and manipulate a Time object. Compilers and IDEs typically invoke the linker for you after compiling your code.

Fig. 9.4 Compilation and linking process that produces an executable application.

Compiling Programs Containing Two or More Source-Code Files

In Section 1.10, we demonstrated how to compile and run C++ applications that contained one source-code (.cpp) file. To perform the compilation and linking processes for multiple source-code files:

  • In Microsoft Visual Studio, add to your project (as shown in Section 1.10.1) all the headers and source-code files that make up a program, then build and run the project. You can place the headers in the project’s Header Files folder and the source-code files in the project’s Source Files folder, but these are mainly for organizing files in large projects. We tested the book’s programs by placing all the files for each program in the project’s Source Files folder.

  • For GNU C++, open a shell and change to the directory containing all the files for a given program, then execute the following command:

    
    g++ -std=c++14 *.cpp -o ExecutableName
    

    The *.cpp specifies that you wish to compile and link all of the source-code files in the current directory—the preprocessor automatically locates the program-specific headers in that directory.

  • For Apple Xcode, add to your project (as shown in Section 1.10.3) all the headers and source-code files that make up a program, then build and run the project.

For further information on compiling multiple-source-file programs with other compilers, see the compiler’s documentation. We provide links to various C++ compilers in our C++ Resource Center at http://www.deitel.com/cplusplus/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset