0

I am trying to use my class in main function with hooking up

#include "Square/Square.h"

but if I do it, I have error

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

But if I use #include Square/Square.cpp all works perfect

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
Barbatosso
  • 77
  • 8
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Apr 30 '17 at 11:35
  • The second version isn't even a valid include directive. So I highly doubt it "works" – StoryTeller - Unslander Monica Apr 30 '17 at 11:36
  • 3
    Please tag sensibly. C and C++ are different languages – sjsam Apr 30 '17 at 11:36
  • 1
    you never should include .cpp files. Error you get is link-time error. – Swift - Friday Pie Apr 30 '17 at 11:38
  • I don't understand why questions like this get voted down. Everyone has to start somehere, and new developers don't know why something isn't working and don't even know what to search for to find the answer. – Andrew Henle Apr 30 '17 at 12:00
  • @Andrew Henle I didn't downvote but people do that, mostly because research wasn't done, simple googling the error text gives link to aswer that was already given. by the way, output doesn't match gcc, neither author specified build environment he uses (CodeBlocks, mayhaps?), so further help is impossible to give properly – Swift - Friday Pie Apr 30 '17 at 13:27

1 Answers1

1

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
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56