• Advertisement

Archived

This topic is now archived and is closed to further replies.

Link error driveing me crazy!

This topic is 5753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I´m includeing ParticleSystem.cpp in the main file and if I create an object of the ParticleSystem-class then I get the link errors below, why? I havnen´t included it twice, declared/defined the functions twice either.
  
Linking...
projectx.obj : error LNK2005: "unsigned long * g_clrColorFade" (?g_clrColorFade@@3PAKA) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "struct D3DXCOLOR * g_clrColor" (?g_clrColor@@3PAUD3DXCOLOR@@A) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: __thiscall ParticleSystem::ParticleSystem(unsigned long,unsigned long,float)" (??0ParticleSystem@@QAE@KKM@Z) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: __thiscall ParticleSystem::~ParticleSystem(void)" (??1ParticleSystem@@QAE@XZ) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: long __thiscall ParticleSystem::InvalidateDeviceObjects(void)" (?InvalidateDeviceObjects@ParticleSystem@@QAEJXZ) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: long __thiscall ParticleSystem::Render(struct IDirect3DDevice8 *)" (?Render@ParticleSystem@@QAEJPAUIDirect3DDevice8@@@Z) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: long __thiscall ParticleSystem::RestoreDeviceObjects(struct IDirect3DDevice8 *)" (?RestoreDeviceObjects@ParticleSystem@@QAEJPAUIDirect3DDevice8@@@Z) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "public: long __thiscall ParticleSystem::Update(float,unsigned long,struct D3DXCOLOR const &,struct D3DXCOLOR const &,float,struct D3DXVECTOR3)" (?Update@ParticleSystem@@QAEJMKABUD3DXCOLOR@@0MUD3DXVECTOR3@@@Z) already d
efined in ParticleSystem.obj
Debug/projectx.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

projectx.exe - 9 error(s), 0 warning(s)
  

Share this post


Link to post
Share on other sites
Advertisement
Don''t include a .cpp, because you''ll make the code get generated twice. It will get compiled once as the .cpp, and once as the file that includes it.

Move the implementation to a .cpp file, leave the declaration in a .h file, and include only the .h file.

Share this post


Link to post
Share on other sites
Now, I´m includeing the ParticleSystem.h and not the .cpp-file, but I still get this link error. Why?!

  
--------------------Configuration: Project X - Win32 Debug--------------------
Linking...
projectx.obj : error LNK2005: "unsigned long * g_clrColorFade" (?g_clrColorFade@@3PAKA) already defined in ParticleSystem.obj
projectx.obj : error LNK2005: "struct D3DXCOLOR * g_clrColor" (?g_clrColor@@3PAUD3DXCOLOR@@A) already defined in ParticleSystem.obj
Debug/projectx.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

projectx.exe - 3 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
I´ve fixed it now. The cause was that I included ParticleSystem.h in the .cpp-file.

Share this post


Link to post
Share on other sites
To include ParticleSystem.h in ParticleSystem.cpp was neccesary so I still get that link error. Why?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Make the variables static and they will only be created once...

Share this post


Link to post
Share on other sites
Use the preprocessor (#ifndef, #define, #endif) to make sure the stuff in the .h only gets included once.

Share this post


Link to post
Share on other sites
Use extern on globals. Search the boards for more info. There''s also an article on this in the reference section.

Share this post


Link to post
Share on other sites
http://www.gamedev.net/reference/articles/article1798.asp. And learn to use search.

Share this post


Link to post
Share on other sites
ParticleSystem.h file:



  
#ifndef PARTICLESYSTEM_H_
#define PARTICLESYSTEM_H_

//... code here ....

// and remember to use extern on globals like this:

extern unsigned int plaaplaa;


#endif


Share this post


Link to post
Share on other sites
peter86,

Trying cleaning (deleting) your project''s intermediate files. Goto the "Build" menu and click "Clean."

Sometimes VC++ gets screwed up when doing incremental builds/pre-compiled headers and the intermediate files need to be killed.

Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites
I don''t know which compilers other than my MSVC6 support #pragma once. That''s why you should use preprocessor if you want your headers to compile as expected on other compilers.

However, even with the preprocessor defines, the compiler must parse your .h file, even if ignores everything that''s there. It''s not a problem if you have several hundred lines in your .h file, but standard headers have thousands of lines and each midl-generated .h file disables the warning "more than 64k source lines" in its beginning. Guess what that is for. The beauty of #pragma once is that the compiler will not include your .h file twice, as opposed to including it as many times as requested and ignoring its 1 MB contents.

Share this post


Link to post
Share on other sites
Oh, so you''re saying with #pragma once, if the file has already been included, the compiler just closes the file right there, whereas with #ifdef/#endif, the compiler still scans the entire file until it reaches the #endif? That makes sense. I use #ifdef/#endif, but I guess it would make sense to use #pragma once if available then.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Seeing as the whole point of the pragma is to optimize compilation times, I doubt it. Heathen that I am, I'd say it's a silly thing to have removed from gcc.

And as to not using it, I suppose I should have searched before posting originally, but now that I have I'd rather use the solution everyone has suggested for this problem:

    
#pragma once
#ifndef <i>_FILE_H</i>
#define <i>_FILE_H</i>

// FILE BODY HERE


#endif


Optimized compilation time and standard operation to boot.

ld

[edited by - liquiddark on April 23, 2002 12:15:53 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
Since awhile ago:

http://www.la.utexas.edu/lab/software/devtool/gnu/cpp/cpp_46.html#IDX82

A few lines down from that marker.




No, that was GNU C that deprecated it. Visual C never did. #pragma are considered compiler specifc and not part of the standard, so they can do that.

So, don''t use it with gcc.

Share this post


Link to post
Share on other sites
quote:

No, that was GNU C that deprecated it. Visual C never did. #pragma are considered compiler specifc and not part of the standard, so they can do that.

So, don''t use it with gcc.


The point is, it''s been deprecated, so if you use it, you''re writing unportable code.

Later,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
AFAIK, #pragma results in unportable code regardless of whether it''s deprecated or not. That''s why MS wrap #pragma once in #if MSC_VER > 1000.

Share this post


Link to post
Share on other sites

  • Advertisement