In my program that uses separate compilation, the only problem I have left is a linker error. To see if it was just a syntax error somewhere obscure, I made a simple "hello world" program that uses separate compilation. I get the same error. The code is below. If someone knows what is wrong, that would be awesome if they could help.
The header file:

//file name: Hello.h
#ifndef HELLO_H
#define HELLO_H

#include <iostream>
using namespace std;

class Hello
{
      public:
             Hello(int x);
};

#endif //HELLO_H

The implementation file:

//file name: Hello.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

Hello::Hello (int x)
{
            cout << "Hello world";
}

The application file:

//File name: HelloApp.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

int main()
{
    char b;
    cout << "Press a key to test: ";
    cin >> b;
    Hello testing(b);
    return 0;
}

I don't know why, but the error I get is " [Linker error] undefined reference to `Hello::Hello(int)' ". Please help. Thanks.

Odd though, I just created this. It gave me no linking error >< Even with a char passed to an int

You need to compile both cpp files in the same compilation:

$ g++ HelloApp.cpp Hello.cpp -Wall -o HelloApp

Or, on an IDE (like VS or Code.Blocks), you would need to add all the .cpp files to the list of "source files" for the project (or solution).

Thank you so much! That is exactly what the problem was! I just have to build my makefile. I built it and ran it, but I'm not sure exactly what it is supposed to do.

It says "make: Nothing to be done for 'SSmakefile'." How is the program run? And is this the right result?
Here is my Makefile:

//File Name: SSmakefile

##TARGET: Dependencies
##    Instructions


SumnerApp1.exe: SumnerApp1.o SSassg1.o
    g++ SumnerApp1.o SSassg1.o -o SumnerApp1.exe
SumnerApp1.o: SumnerApp1.cpp SSassg1.h
    g++ -c SomeApp1.cpp
SSassg1.o: SSassg1.cpp SSassg1.h
    g++ -c SSassg1.cpp
clean: 
    rm -f SumnerApp1.o SSassg1.o SumnerApp1.exe

Is it just not compiling the member programs? (The programs are SumnerApp1.cpp, SSassg1.cpp, and SSassg1.h)

Edited 5 Years Ago by iamthesgt: n/a

This question has already been answered. Start a new discussion instead.