Compiling Multi-Part Programs

The objective for this lab assignment is to learn how to compile programs that are in multiple files. In the next lab assignment the student will learn how to use the Make program to make this process simpler and more efficient.


Do each of the following steps, carefully, and in order. Observe what happens. Write your answers to questions A-E on a sheet of paper and submit your answers at the beginning of the next lab section.


1 - Create a new (empty) directory.

2 - Create a file named "header.h" containing the following two lines:

#include <iostream>
void nice_print();

3 - Create a file named "parta.cpp" containing the following:

#include "header.h"
using namespace std;
int  main ( )
{
     cout  <<  "Hello world!"  <<  endl;
     return 1;
}

4 - Use the following command to compile your code:
     c++ parta.cpp

5 - Do an "ls -l" directory command.
Question A - What is the name of the executable program that was created?

6 - Run that program and watch what happens. Then, remove that executable program file.


7 - Edit parta.cpp to include a new line that will call a function named "nice_print". The declaration for that function is already in the header file, and the code will be in a different file.

#include "header.h"
using namespace std;
int  main ( )
{
     cout  <<  "Hello world!"  <<  endl;
     nice_print();
     return 1;
}

8 - Type the following commands:
     ls -l
     c++ parta.cpp

Question B - In English, explain that error message.

9 - Type the following commands:
     ls -l
     c++ -c parta.cpp
     ls -l

Question C - What file was created? And, is it executable?


10 - Create a new file named "partb.cpp" containing:
#include "header.h"
using namespace std;
void nice_print()
{
   int indent, cnt;
   for (cnt=1; cnt<=5; cnt++)
     {
      for (indent=0; indent < cnt; indent++)
         cout << "\t";
      cout << "hello\n";
     }
}

11 - Type the following commands and observe the results.
     ls -l
     c++ -c partb.cpp
     ls -l
     c++ parta.o partb.o
     ls -l

Question D - What executable file was created?

12 - Run the executable file.


13 - Edit partb.cpp as follows

#include "header.h"
using namespace std;
void nice_print()
{
   int indent, cnt;
   for (cnt=1; cnt <= 555; cnt++)
     {
      for (indent=0; indent < cnt; indent++)
         cout << "\t";
      cout << "hello\n";
     }
}
14 - Recompile just the part B file:
     c++ -c partb.cpp

15 - Re-run the executable file.

Question E - Why did the output not change?