Author Topic: Linker multiple definitions  (Read 3963 times)

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Linker multiple definitions
« on: July 28, 2008, 10:16:47 am »
Hi everyone,

I've recently been having linker problems when compiling projects with classes. The linker (GNU's LD i believe) delcares that member functions have been defined multiple times, and I cannot see any apparent reason for this.

To test the problem, I created a small project with a single basic class, with the declaration and definition in separate ".h" and ".cpp" files respectively. I've coded for the whole "#ifndef" thing in both files. The main() function simply creates an instance of the object, performs a few functions, and outputs a result. However, on compilation of the project, the linker tells me that I have multiple definitions of all my member functions. I've had this happen with other projects with classes in as well. The really weird thing is that when i compile it with g++ from the command line, the project compiles successfully and everything works fine.

I've tried LD's option to allow multiple definitions but this didn't work for some reason. I will post source code here ASAP, but at the moment I'm away from my home computer so you will have to bear with me. Oh and I'm running this all on Win XP Pro SP3 if that's worth anything.

Any ideas on what I need to do to get this to work?

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #1 on: July 29, 2008, 08:53:52 am »
Source code:

main.cpp:

#include <iostream>
#include "class.cpp"

using namespace std;

int main()
{
    /*myClass a;

    for (int i=0;i<10;i++)
    {
        a.Increment();
    }

    for (int i=0;i<3;i++)
    {
        a.Decrement();
    }
    cout << a.Value() << endl;*/
    return 0;
}

class.h:

#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

class myClass
{
    int m_x;

public:
    myClass(int x);
    myClass();
    ~myClass();
    void Increment();
    void Decrement();
    int Value();
};

#endif // CLASS_H_INCLUDED

class.cpp:

#ifndef CLASS_CPP_INCLUDED
#define CLASS_CPP_INCLUDED

#include "class.h"

myClass::myClass(int x)
{
    m_x = x;
}

myClass::myClass()
{
    m_x=0;
}

myClass::~myClass()
{
}

void myClass::Increment()
{
    m_x++;
}

void myClass::Decrement()
{
    m_x--;
}

int myClass::Value()
{
    return m_x;
}

#endif // CLASS_CPP_INCLUDED

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Linker multiple definitions
« Reply #2 on: July 29, 2008, 09:34:56 am »
Gnu ld is correct (as was to be expected). You compile the class sourcefile twice by adding it to the project and including it from the main file.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #3 on: July 29, 2008, 10:07:25 am »
But I've tried commenting out the include and was then told the class wasn't defined at all. I always thought you had to include all source files  :?...

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #4 on: July 29, 2008, 10:19:58 am »
If it's worth anything I also tried compiling projects in Dev-C++ with classes and recieved the same problem.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Linker multiple definitions
« Reply #5 on: July 29, 2008, 01:16:07 pm »
Well, if you include the header file, not the source file, it will work.

This isn't related to any particular IDE, compiler, or linker. You just aren't doing things right. Read a C/C++ tutorial on how to use #include to understand what it does.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #6 on: July 29, 2008, 01:29:01 pm »
Ok, I'll give that a go then. It just struck me as a little odd that it worked fine when compiling from the command line using g++. But I'll do what you suggest and get back to you. Thanks for your help.

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #7 on: July 29, 2008, 01:46:42 pm »
Ok, I understand it now. The compiler automatically compiles all the source files, so they don't need to be included. The header file needs to be included so that the main function knows the class exists. When I compiled with g++, I didn't tell g++ to compile the class.cpp separately, but since it was included in the main.cpp file it was compiled anyway. Now I jsut need to try it out.

Offline sggarps

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Linker multiple definitions
« Reply #8 on: July 29, 2008, 09:08:05 pm »
Yeah adding the .h instead of the .cpp worked  :), thanks very much for your help :).