This code
#include "Square/Square.h"
means that the contents of the file "Square/Square.h" are included at that point in your code. That one line is replaced by the entire contents of that file.
Files such as "Square/Square.h" - a header file - are usually used to declare things - it's a way of saying how something is used, or how it works. Header files do not usually provide the what, though - that's the definition, apparently in this case of a square.
Have you ever traveled internationally? Well, when you enter a country you might have to fill out a customs declaration - which declares that you have something else. The declaration isn't the thing itself - it just says you have that thing. That's normally what a header file does - it declares things that actually exist somewhere else.
So you get these error messages:
Undefined symbols for architecture x86_64:
"Square::calculateSquare()", _main in main-cac286.o
"Square::Square(double)", referenced from: _main in main-cac286.o
"Square::~Square()", referenced from: _main in main-cac286.o
"Square::calculateSquare()", referenced from: _main in main-cac286.o
"Square::Square(int)", referenced from: _main in main-cac286.o
"Square::~Square()", referenced from: _main in main-cac286.o
because your compiled executable doesn't have the thing available to it - the header file only declared it, it didn't define or provide it.
But if I use #include Square/Square.cpp
all works perfect
Because that file defines the thing - the definition of "square" in this case.
As others have noted, you shouldn't #include Square/Square.cpp
- for a lot of reasons.
The simplest solution is to compile both (all) your *.cpp
files at once. Using g++
, you'd do something like:
g++ main.cpp Square/Square.cpp -o myprogram