This error is driving me nuts

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

This error is driving me nuts

Post by twentytortures »

When compiling I get this error:

Code: Select all

obj/Debug/main.o: In function `irrAllocator':
/home/mike/irrlicht/include/irrAllocator.h:26: multiple definition of `device'
obj/Debug/event.o:/home/mike/projects/irrtemp/event.cpp:13: first defined here
obj/Debug/main.o: In function `irrAllocator':
/home/mike/irrlicht/include/matrix4.h:697: multiple definition of `click'
obj/Debug/event.o:/home/mike/projects/irrtemp/event.cpp:5: first defined here
collect2: ld returned 1 exit status
I used to compile fine until I decided to put my event handler in a separate .cpp file, then I started getting this error. I put everything back like it was before I moved it to the separate file and I'm still getting the error.

What's going on?[/code]
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Post by wildrj »

can you please post your event.cpp so we can have a looksy.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

Code: Select all

#include <irrlicht.h>

using namespace irr;

IrrlichtDevice* device = 0;
core::position2d<s32> click = core::position2d<s32>(0,0);

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_X:
				{
                    click = device->getCursorControl()->getPosition();
				}
				return true;
			}
		}

		if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
		{
			switch(event.MouseInput.Event)
			{
			case EMIE_LMOUSE_PRESSED_DOWN:
				{
					click = device->getCursorControl()->getPosition();
				}
				return true;
			}
		}

		return false;
	}
};
Could it be because of the name of the file? I put everything into one .cpp and removed the event .cpp from my project and it compiled.
Catprog
Posts: 164
Joined: Wed Jan 31, 2007 9:07 am
Contact:

Post by Catprog »

what about main.cpp
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

what is IrrlichtDevice* device = 0; doing in event receiver? i think this might be an error. Keep device, scene manager and driver 'in one place'. If you need to use them remotely then make their structure (i made VideoEngine object myself) and pass it to that constructors which class needs them.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

make the device extern, or make a .h file where you define it.
probably same for click.

Code: Select all

extern IrrlichtDevice* device = 0;
extern core::position2d<s32> click = core::position2d<s32>(0,0);
//Tells the linker and compiler that the variables are not actual parts of this .cpp but another .cpp
If you don't have anything nice to say, don't say anything at all.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

I've tried making them extern but it's of no use. The reason device is set to 0 is because it won't compile if I don't. I get an error saying "device not declared in this scope" because the variable is declared in main.cpp but is used in event.cpp. I'll post the contents of main.cpp when I get home.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

If you make them extern in both files, it wont compile either. check this here thread for the post where i try to explain what and why and how.
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=20517
If you don't have anything nice to say, don't say anything at all.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

I only did the extern in event.cpp and I already found that page, but thanks anyways. I'm seriously thinking it has something to do with the file being named event. I'll try naming it something else when I get home. Like i said before, it compiles fine if I put all the code in main.cpp.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

Sorry it took so long for me to post this, I just don't have much time between school and work. I finally had some time to play with it. The code is now organized in two files, main.cpp and myReceiver.cpp. I'm still getting the same error. Here's something strange though, I got it to work.

I'm using codeblocks svn3816 which is fairly new. I noticed that if I removed myReceiver.cpp from my project it compiled just fine. I think it's probably a bug in codeblocks. Pretty strange.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

It's very rarely the tool which has the bug. In your case the error message is quite clear: Those variables have been defined more than once. My guess would be that you include that .cpp file somewhere, as that would cause that problem. And it looks like you do not yet understand the difference between declarations and definitions in c++ (looks more like java) so you might read up some more on that topic.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

There are only two .cpp files and only one of those files has declares for my variables. They are definitely only declared once. The file is still #included in my main.cpp but I only get errors when I add it as part of my codeblocks project. If it was an error with the code, wouldn't I still get the error?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

When both your main.cpp and event.cpp are part of your project and you include event.cpp additionally in main.cpp then it will be defined twice. Each cpp file in your project results in it's own object file. And any file included can just be seen as part of that file (it's as if you did copy&paste the whole content at the position of the #include). So you will get now an event.o where those variables are defined and additionally you will have an main.o which does include the whole event.cpp and therefore does also hold all that definitions. And when the object files are linked that definitions are now there twice with the same name and you get therefore those linker errors.

Simply don't include a .cpp file, you never have to do so. Put declarations in a header and include that. If you're not sure what's up with that declaration/definition stuff do read up on that. You will simply need to know about that when working with c++.
twentytortures
Posts: 41
Joined: Thu Sep 28, 2006 4:34 am
Location: Eaton, Colorado, United States

Post by twentytortures »

Thanks very much! I didn't realize it worked that way. I'm not used to compiling things in projects, I usually just have a bunch of files.
Post Reply