Objectives

We would like to :
  1. Discuss implementing projects into multiple files

Notes

  • When dealing with Multiple Files
  • We will do this because it allows us to
  • We started with decConvert.cpp
  • We split these into three files
    1. The header file
      • Again, this holds only
        • Function prototypes
        • Constant definitions
        • Type definitions
      • But only the things we want to export
      • We need to include the whatever we need for the function (string)
      • We usually don't put using namespace std
      • We need to add #pragma once to the top.
      • The file converters.h became
        #pragma once
        
        #include <string>
        
        int StringToInteger(std::string num); 
    2. The implementation file
      • This holds the code that implements the functions.
      • As well as code for any supporting functions.
      • No restrictions on what goes in here.
      • The file converters.cpp contains
        #include <iostream>
        #include "converters.h"
        
        using namespace std;
        
        int StringToInteger(string num){
             int value{0};
             size_t i;
        
             for(i = 0; i < num.size() ; ++i) {
                 value = value * 10 + (num[i] -'0');
             }
        
             return value;
        } 
      • We will compile this with g++ -o converters.o -c converters.cpp
      • The -c flag says produce an object file, but no executable.
    3. Finally the main program
      • We need to #include "converters.h" at the top.
      • for include
        • <file> means look in the standard locations
        • "file" means look in the local directory.
      • We do not include converters.cpp, we will link with the .o file from above.
      • The file converTester.cpp becomes
        #include <iostream>
        
        #include "converters.h"
        
        using namespace std;
        
        int main() {
        
            cout << StringToInteger("345") << endl;
        
            return 0;
        } 
      • To compile this
        1. We need converters.o from above
        2. g++ -o converTester converTester.cpp converters.o
  • But that is too much work so we added the following lines to the Makefile
  • We are now relying on three parts of the compiler
  • All three are part of the g++ command.