Advertisement

Linking trouble with C++ classes

Started by July 18, 2001 01:26 AM
1 comment, last by fairie 23 years, 5 months ago
Errr... I'm using DJGPP (gcc) with the RHIDE editor for compiling. However, whenever I try to use classes I get multiple definition errors... For example (several imaginary files): File useless.h:
    

#ifndef USELESS_INIT
#define USELESS_INIT


class useless
{
public:
  useless();
  void aFunction(int);

private:
  int aVar;
};

#endif

  
File useless.cpp:
  

#include "useless.h"


useless::useless()
{
  aVar = 0;
}

void useless::aFunction(int newVal);
{
  aVar = newVal;
  return;
}

  
File example.cpp:
  

#include "someclass.h"


int main()
{
  useless anObject;
  anObject.aFunction(3);

  return 0;
}

  
Pretty useless class mmm? ^_^ Each file will compile fine, but when I try to link the source files together, I get an error something like:
quote: Error: useless.o: In function 'useless::useless(void)': useless.cpp(3) Error: multiple definition of 'useless::useless(void)' o:useless.cpp(3) Error: first defined here
for _every_ function in _every_ class. o_O What confuses me is that they compile and link fine with Bloodshed Dev-C++, so... What am I doing wrong? Any help would be greatly appreciated. Eck! Sorry for the long winded post.. Thanks in advance. Edited by - fairie on July 18, 2001 2:46:46 AM
Well, good news; it isn''t your code. I copied those fake sample files and they compiled fine when I did it myself at the command line. However, when I did it in RHIDE, I got the same errors. I noticed that when RHIDE was doing the linking part (with the object files) it put someclass.o twice (like this: gcc -o something.exe example.o someclass.o someclass.o). You can solve this problem by NOT putting header files (just the source files) in your project (for example, this project would have only example.cpp and someclass.cpp and not someclass.h). It compiled with no errors after that. I''m not sure why RHIDE is like this, but I''m willing to bet that the docs say ''Don''t put header files in your project'' somewhere (I don''t use RHIDE, so I don''t know...)

Hope that helped!
Advertisement
Ah! Thank you greatly! Apparently RHIDE doesn''t exclude header files from the linking process automatically, and they must be tagged as such manually. (I don''t normally use RHIDE either, but currently it is my only way of visually debugging: I haven''t gotten a handle on using the command line debugger with Bloodshed, and my attempts to get Insight have... well, I''ve never been able to find out how to get the proper files) I''m rather impressed at the speed of your reply, thanks for the help.

This topic is closed to new replies.

Advertisement