Advertisement

Multiple Definition

Started by August 15, 2001 10:57 PM
6 comments, last by Flanders 23 years, 6 months ago
Hi, I''ve already searched and teh threads related to this problem are of no help! I''m getting the errors while compiling a NeXe tutorial: e:\c++ development\dx8manager\graphics\texture.o(.text+0x8):texture.cpp: multiple definition of `CDirect3D::Create(CWindow *)'' e:\c++ development\dx8manager\graphics\main.o(.text+0x8):main.cpp: first defined here I''m using Dev-C, I''ve been able to compile this same code in a non-OO manner.... it seems that the re-arrangement has caused a linker problem. I''ve never been able to get these problems fixed. All of the code in the file Direct3D.h is gaurded by a #ifndef statement. Anyway, I''m pulling out all my hair over this, so any help is appretiated.. thanks!
- Flanders -
You''re probably defining CDirect3D::Create(CWindow *) in a header file, which will mean it gets compiled twice. If so, move it to a source file.
Advertisement
Ok...

Yes, the class CDirect3D is defined all in one file, CDirect3D.h

the class CTexture is including this file.

I only ever see one definition of this method, and this is in the CDirect3D.h file - now the code is like this:

class CDirect3d
{
.
.
.
bool Create(CWindow *fp_pWindow);
.
.
.
};


bool CDirect3D::Create(CWindow *fp_pWindow)
{
.
.
.
}


As far as I knew, this was ok? The other thing I should mention is that I''m getting an error message like this for each method in CDirect3D ...

thanks for the help!
- Flanders -
Did you understand what I said?

#including a file just includes all its contents in the file that includes it. That means, if several .CPP files (for example, texture.cpp and main.cpp in your case) include a .H file with a function in it (for example, CDirect3D.h contains CDirect3D::Create), then it gets compiled twice, once for each unit. This is wrong. The linker will notice it has 2 copies. Which one should it use? It gets confused. The answer is to put functions in a CPP file. Then it only gets compiled once. The linker sees a single copy. Problem over.

Quick rundown of What To Do:

Class definitions in header files.
Extern global declarations in header files.
Function prototypes in header files.
Function bodies in a single code file.
Actual global variable declarations in a single code file.
Static data members defined in a single code file.

Asaari had a similar problem here.
Yes I understand what you said.

I have all of the function definitions for a class in one file.

Dev-C would not compile this. Did you under stand that?

I tried it under MS Visual C++, and it compiles - go figure.

I think Dev-C is being picky.
- Flanders -
You obviously didn''t understand. You shouldn''t have the function definitions and the class definition all in one file. That is ''wrong'' in 99% of situations. Including yours. When you include that file twice, you compile the functions twice and the linker sees a collision. This is an error, regardless of whether some compiler somewhere manages to deal with it or not. Move the function bodies out of the header and your problem is over.
Advertisement
Actually, thats how it was origionally, and that is what would not compile.

I understand how C++ code is supposed to be layed out, I''ve been compiling code like this under unix for quite some time now.

I also know that the code would not compile under Dev-C.

If you''re so certain that what you''ve written is absolutely correct, then by all means try it yourself under Dev-C and see if it works for you.
- Flanders -
*sigh* Read up on the linking process, especially the concept of compilation units, the difference between compiling and linking, and learn for yourself why you shouldn''t put a function definition in a header, rather than insisting I''m wrong when I''m not the one who can''t get his code to compile. If you had it separated out in the first place as it should have been, and it wouldn''t compile, then you made a mistake somewhere else.

(Note: I gave the same advice to Asaari and his code now works. That''s because I know what I''m talking about.)

This topic is closed to new replies.

Advertisement