1

So I'm in a basic programming class, and we're learning how to link files together. The problem is I've run into an error that no one seems able to fix. I've already been to my professor, student assistants, and the programming assistance lab on campus, no luck.

I've also searched through at least 10 different posts here relating to multiple definition errors, but in every case it's either:

  1. An attempt to #include a .cpp file or

  2. Function definitions are inside the header file.

Neither of these cases apply here, as you can see (I have removed all comments to make the code easier to read):

Header file: square.h:

#ifndef SQUARE_H
#define SQUARE_H

class Square
{
    private:
            float side;
    public:
            void setSide(float);
            float findArea();
            float findPerimeter();
};

#endif

Function definitions file: square.cpp

#include "square.h"
#include <iostream>
#include <cstdlib>

using namespace std;

void Square::setSide(float length)
{
  side = length;
}

float Square::findArea()
{
  return side * side;
}

float Square::findPerimeter()
{
  return 4 * side;
}

Program file: test1.cpp

#include "square.h"
#include <iostream>

using namespace std;


int main()
{
    Square  box;
    float   size;

        cout << "\n\nHow long is a side of this square?: ";
        cin >> size;

        box.setSide(size);

        cout << "\n\nThe area of the square is " << box.findArea();

        cout << "\n\nThe perimeter of the square is " << box.findPerimeter();

        cout << endl << endl;

        return 0;
}

And finally the error whenever I try to compile:

/tmp/ccVaTqTo.o: In function `Square::setSide(float)':
square.cpp:(.text+0x0): multiple definition of `Square::setSide(float)'
/tmp/cc4vruNq.o:test1.cpp:(.text+0x0): first defined here
/tmp/ccVaTqTo.o: In function `Square::findArea()':
square.cpp:(.text+0xe): multiple definition of `Square::findArea()'
/tmp/cc4vruNq.o:test1.cpp:(.text+0xe): first defined here
/tmp/ccVaTqTo.o: In function `Square::findPerimeter()':
square.cpp:(.text+0x20): multiple definition of `Square::findPerimeter()'
/tmp/cc4vruNq.o:test1.cpp:(.text+0x20): first defined here
collect2: ld returned 1 exit status

Compile command, just for reference g++ test1.cpp square.cpp -o testfile

Using a makefile does not seem to make any difference, I just end up staring at the exact same compile error. I've tried using two different makefiles:

Makefile 1

square: test1.cpp square.cpp
        g++ test1.cpp square.cpp -o square

Makefile 2

square: test1.o square.o
        g++ test1.o square.o -o square

test1.o: test1.cpp
        g++ -c test1.cpp

square.o: square.cpp
        g++ -c square.cpp

Right now all I know is that this is some sort of linker error, but I have no idea what to do about it. Everyone I've asked has said my code is correct. But clearly SOMETHING is wrong.

Any help would be greatly appreciated. :D

9
  • Check the "stupid" stuff: Are you looking at the code in one directory and compiling it in another? cat the files to check they really have the content you think. Create a new directory, copy only those files above there and try to compile them again... Commented Oct 30, 2013 at 23:34
  • Missing definitions are more prominent when link-lines are not properly ordered; not multiple. The only think I would change in your link line is putting the -o square first, but that shouldn't change your outcome as-is. This code works and builds as expected on my rig, so you likely have stale content somewhere from before you factored out the Square code and header. Commented Oct 30, 2013 at 23:36
  • What is your compile sequence? | g++ test1.cpp square.cpp -o square && make #Makefile2 works for me. Commented Oct 30, 2013 at 23:37
  • No change. Interesting that it worked for you, though. Right now I'm compiling in a Unix environment, what would be a simple way to compile in a different environment? Commented Oct 30, 2013 at 23:37
  • Did you remove all of the comments before or after you last saw these errors? In other words, do you get these errors with the very code you're showing us? Commented Oct 30, 2013 at 23:39

2 Answers 2

4

Okay. Making a new, clean directory, and copy/pasting the code into new files (with the same filenames as the old files allowed it to finally compile. For some reason, I guess the compiler just didn't like my old files.

Sign up to request clarification or add additional context in comments.

Comments

0

I think maybe this can help

Try the makefile as follow:

square: test1.o square.o
        g++ test1.o square.o -o square

test1.o: test1.cpp square.h
        g++ -c test1.cpp

square.o: square.cpp square.h
        g++ -c square.cpp

EDIT if it doesn't work try to run preprocess only:

g++ -E test1.cpp

and

g++ -E square.cpp

check the outputs to see if it took the correct files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.