Why did I have to compile two different CPP source files in order to not get a compile error?

Hello Everyone,

My C++ application compiled fine for me yesterday, but I was wondering why I had trouble compiling the CPP source files separately than together. I read only that you don’t have to compile them at the same time. Anyway, I have attached a picture of the problem when I don’t compile together. And I also attached my header files and CPP’s source code.

#include <iostream> 
#include <string>  
using namespace std; 
 
class Employee 
{  
   private: 
   string firstname; 
   string lastname; 
   int salary; 

   public:     
   void setFirstName(string);
   string getFirstName(); 
   void setLastName(string); 
   string getLastName(); 
   void setSalary(int);  
   int getSalary();  
};  

#include "Employee.h" 
#include "stdafx.h"   

void Employee::setFirstName(string f) 
{ 
    firstname= f; 
} 

string Employee::getFirstName()
{
     return firstname; 
}

void Employee::setLastName(string l) 
{ 
    lastname= l; 
} 

string Employee::getLastName() 
{ 
    return lastname; 
} 

void Employee::setSalary(int s)
{  
    salary= s; 
} 

int Employee::getSalary() 
{  
   return salary; 
} 

#include "Employee.h" 

#include "stdafx.h" 

#include <iostream>

#include <string> 

#include <cstdlib>

using namespace std; 

 
int main() 
{ 
 
   Employee robert; 
 
   robert.setFirstName("Robert"); 
   robert.setLastName("Smith");
   robert.setSalary(100); 

  
 
    cout << "Salary for " << robert.getFirstName() + " " + robert.getLastName() << " is " << robert.getSalary() << endl; 
 
    cout << "Hit any key to continue" << endl;  
    system ("pause"); 
 
    return 0; 
} 

Here is the first command prompt that comes up when I don’t compile the Employee.cpp and Assignment 10.cpp together: (I know I excluded assignment 10 and my name- remember that’s not important right now)

Now here is the good compile error free on the command prompt:

I am not sure but I think those errors are linker errors, not compiler errors. The thing to learn about is linking. Compiling and linking are separate things. First the compiler executes then the linker executes.

The compiler converts the C++ source language to machine code but the machine code cannot be executed directly; it must be linked with the code from the other files. The output of the C++ compiler is object files (looks like the compiler you are using uses “o” for the extension for the object files). Every input file (.c or .cpp) for the compiler is compiled to an object file.

When all the source files have been compiled to object files, the linker combines all the object files into an executable file. The use of object files and the linker allows us to have multiple source files. It is even possible to combine object files from more than one compiler; that is, from source files in more than one language.

When you want to compile just one file then just execute the compiler, don’t execute the linker too. I don’t know how to do that using the compiler you are using but it might just be a matter of the compiler switches. If you don’t compile all the source files then the linker won’t have all the object files necessary to create a complete executable file and I think that is what the linker is trying to tell you.

@SamuelCalifornia,
But Everytime I compile the files separately, it gives me compile errors that you see in the first command prompt. It only works when I compile them together. What can I do to solve this problem?

@SamuelCalifornia
I’m researched my problem and found a solution that I already incorporated when compiling and linking. You were right about it being a linking error instead of a compile error. However, people keep telling me that I CAN COMPILE SEPARATELY, but the compiler obviously what’s to know where my get/accessor methods are at hence the linker error 'Undefined reference to … ’ Here was a solution to my problem( http://stackoverflow.com/questions/14557657/linker-error-c-undefined-reference ), but I now need to figure out another solution. Any ideas except putting classes and main application back together?

By default, the compiler will try to compile and link all at once, which is why it’s been wanting all your source files. If instead you want it to just compile – which it can do one file at a time – then you’ll need the -c flag.

g++ -c Assignment10.cpp

g++ -c Employee.cpp

The first command will produce “Assignment10.o”, and the second command will produce “Employee.o”.

After that, to produce the final executable, provide each of the object files (the .o files) to g++.

g++ Assignment10.o Employee.o -o Assignment10.exe
1 Like

It is not the compiler that wants to know where the method (implementations) are, it is the linker. When compiling a single C++ source file among multiple files, just don’t link. Please don’t put “classes and main application back together” or do anything else to solve this “problem” that is not a problem. The only thing you need to do to solve the problem is to learn about the linker.

Yes, that’s it. It has been a while since I have worked with C++ so I forgot how it worked. I tried to find the switch for linking and could not find it and I had forgotten that the switch is for just compiling.

@Jeff_Mott,

The compiler compiles both the Assignment10 and Employee fine, but I have a really stupid question to ask. Are you ready for it? Do you know why when I tried to run the Employee.h file, it doesn’t output anything? I know because it’s just a setting up get/set accessor methods to be used in a later class. There isn’t anything to be output. I just thought I asked that question. I sometimes feel like the world has stop asking questions because people are afraid they will get judged.

A header file isn’t supposed to output anything. The cpp files (generally) contain all the real code, while h files contain the interface information needed to use that real code. We can compile Assignment10 separately from Employee, but Assignment10 still needs to know some information from Employee just to be able to use it. It needs to know the size of an Employee object so it can allocate the right amount of space for one, and it needs to know the parameters and return types of functions. So our C++ code is split between declarations and definitions. Declarations in the Employee header file contain just enough information to tell other cpp files what they need to know to use an Employee object.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.