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

Started by
6 comments, last by jpetrie 17 years, 6 months ago
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/
GamesTopica.Net- http://www.gamestopica.net
Advertisement
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

or just make the function "inline". Seems pretty tiny.
daerid@gmail.com
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.
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.
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?
GamesTopica.Net- http://www.gamestopica.net
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?
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.

This topic is closed to new replies.

Advertisement