Sign in to follow this  
Extrakun

[Visual C++] A problem with multiple symbols being defined

Recommended Posts

Hi all, I have this piece of code
#ifndef GUIUtil__
#define GUIUtil__

/*
CEGUI_Util namespace defines the GUIUtil namespace, where utility functions which does not
belong to classes are defined here.

Include this file after all CEGUI includes
*/

namespace GUIUtil
{
	// Load in a layout specified by layoutFileName, and load it into
	// destWindow
	void loadLayout(char * layoutFileName, CEGUI::Window* destWindow)
	{
		CEGUI::WindowManager* Wmgr = CEGUI::WindowManager::getSingletonPtr();
		destWindow = Wmgr->loadWindowLayout(layoutFileName);		
	}
};

#endif 
Which I include in every other classes. When I complied, however, I keep getting:
Quote:
GUIController.obj : error LNK2005: "void __cdecl GUIUtil::loadLayout(char *,class CEGUI::Window *)" (?loadLayout@GUIUtil@@YAXPADPAVWindow@CEGUI@@@Z) already defined in GUIBlackBox.obj GUIGroup.obj : error LNK2005: "void __cdecl GUIUtil::loadLayout(char *,class CEGUI::Window *)" (?loadLayout@GUIUtil@@YAXPADPAVWindow@CEGUI@@@Z) already defined in GUIBlackBox.obj Program.obj : error LNK2005: "void __cdecl GUIUtil::loadLayout(char *,class CEGUI::Window *)" (?loadLayout@GUIUtil@@YAXPADPAVWindow@CEGUI@@@Z) already defined in GUIBlackBox.obj Tutorial_1.obj : error LNK2005: "void __cdecl GUIUtil::loadLayout(char *,class CEGUI::Window *)" (?loadLayout@GUIUtil@@YAXPADPAVWindow@CEGUI@@@Z) already defined in GUIBlackBox.obj
I'm quite sure I have include guards...is there any other thing I shall check for the cause of this error/

Share this post


Link to post
Share on other sites
Alas the problem is the code is put in the header file. Now the compiler adds this code to every object and the linker complains of course.

Simple way out:

Split the util file in header and cpp, and put the code in the cpp file.

Share this post


Link to post
Share on other sites
Quote:
Not actually the GP or FB FAQ, but would be near the top of both if I were running the show
Q. It compiled but I got linker errors. (Or, "what does this error "blah blah blah symbol blah blah blah" mean?)
A. Read the GDNet article about it. If that doesn't answer your question, read it again until you get it. If you're really convinced that it doesn't answer your question, then post the example code and the full error, and Zahlman will respond by quoting directly from the article and showing how it answers your question exactly, and exactly how to fix it. Although he won't be terribly happy about it.

Share this post


Link to post
Share on other sites
Quote:

I'm quite sure I have include guards...is there any other thing I shall check for the cause of this error?


Include guards don't help here. Include guards ensure that a given header file's content will appear exactly once...per translation unit. In the article Zahlman linked above, you'll want to skim "Problem 3" and read "Problem 4" in depth, as it addresses you specific problem.

Share this post


Link to post
Share on other sites
Thanks for the advice, everyone, and sorry for not reading the FAQ in depth.

But, just one thing that I am curious about.

I changed the include guards to


#ifndef GUIUtil
#define GUIUtil


And the code links with no error...

This doesn't make sense, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrakun
Thanks for the advice, everyone, and sorry for not reading the FAQ in depth.

But, just one thing that I am curious about.

I changed the include guards to


#ifndef GUIUtil
#define GUIUtil


And the code links with no error...

This doesn't make sense, right?


Nope, sure doesn't. Sure you didn't add the inline or static keyword to the function?

Share this post


Link to post
Share on other sites
There are various little reasons that can happen; if you full-rebuild (clean, rebuild) the project the error usually reappears. It's not actually gone, you just suppressed it temporarily due a particular transient configuration of the various .obj files the linker works with.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this