Skip to main content
GameDev.net gamedev.net
🔒 Locked

already defined in main.obj

Started by Mozly Apr 18, 2011 at 1:57 AM 12 replies 5.7k views
Original Post
Mozly
Mozly
I am getting this error when I try to compile:

1>Particle.obj : error LNK2005: "struct SDL_Surface * red" (?red@@3PAUSDL_Surface@@A) already defined in main.obj
1>Particle.obj : error LNK2005: "struct SDL_Surface * blue" (?blue@@3PAUSDL_Surface@@A) already defined in main.obj
1>Particle.obj : error LNK2005: "struct SDL_Surface * green" (?green@@3PAUSDL_Surface@@A) already defined in main.obj


Source file:
#include "Particle.h"

Particle::Particle(int X, int Y)
{
x = X + 5 + (rand()%25);
y = Y + 5 + (rand()%25);

frame = rand() % 5;

switch(rand()%3)
{
case 0:
type = red;
break;
case 1:
type = green;
break;
case 2:
type = blue;
break;
}
}


Header File:
#pragma once
#include <SDL.h>

SDL_Surface *red;
SDL_Surface *green;
SDL_Surface *blue;
SDL_Surface *shimmer;

#ifndef PARTICLE
#define PARTICLE


class Particle
{
protected:
//offsets
int x,y;

// current frame of animation
int frame;

//type of particle
SDL_Surface* type;

public:
Particle(int X, int Y);
~Particle(void);
void show();
bool is_dead();
};

#endif


I know its because I include particles.h in this source file and in my main source file (I figured this out because I researched this type of error before positing).
I Really need red, green and blue to be global variables. what can I do to get rid of this error
Hodgman
Hodgman
In one CPP file:SDL_Surface *red = 0;//make a global variable named 'red'
SDL_Surface *green = 0;
SDL_Surface *blue = 0;
SDL_Surface *shimmer = 0;
In a header file (which means copied into multiple CPP files):extern SDL_Surface *red;//declare that there is a global variable named 'red' *somewhere*
extern SDL_Surface *green;
extern SDL_Surface *blue;
extern SDL_Surface *shimmer;
smart_idiot
smart_idiot
Put 'extern SDL_Surface *red, *green, *blue, *shimmer;' in the header file, and put 'SDL_Surface *red, *green, *blue, *shimmer;' inside the source file.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Mozly
Mozly

In one CPP file:SDL_Surface *red = 0;//make a global variable named 'red'
SDL_Surface *green = 0;
SDL_Surface *blue = 0;
SDL_Surface *shimmer = 0;



So I copied that into my source file so it looks like this now
#include "Particle.h"

SDL_Surface *red = 0;//make a global variable named 'red'
SDL_Surface *green = 0;
SDL_Surface *blue = 0;
SDL_Surface *shimmer = 0;

Particle::Particle(int X, int Y)
{
x = X + 5 + (rand()%25);
y = Y + 5 + (rand()%25);

frame = rand() % 5;

switch(rand()%3)
{
case 0:
type = red;
break;
case 1:
type = green;
break;
case 2:
type = blue;
break;
}
}


but now I get this error
1>c:\users\mishu\desktop\documents\visual studio 2010\projects\sdl tut 27 particles engine\sdl tut 27 particles engine\particle.cpp(3): error C2086: 'SDL_Surface *red' : redefinition
1> c:\users\mishu\desktop\documents\visual studio 2010\projects\sdl tut 27 particles engine\sdl tut 27 particles engine\particle.h(8) : see declaration of 'red'

Doesn't that mean that I'm declaring these variable twice?
Mozly
Mozly

Put 'extern SDL_Surface *red, *green, *blue, *shimmer;' in the header file, and put 'SDL_Surface *red, *green, *blue, *shimmer;' inside the source file.


Now that I added extern it works. But I dont get why?
smart_idiot
smart_idiot

[quote name='smart_idiot' timestamp='1303092583' post='4799705']
Put 'extern SDL_Surface *red, *green, *blue, *shimmer;' in the header file, and put 'SDL_Surface *red, *green, *blue, *shimmer;' inside the source file.


Now that I added extern it works. But I dont get why?
[/quote]

Extern is compiler speak for 'Please just take it on faith that this variable exists and that the linker is going to tell you where at some as of yet unknown time and place.'
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Mozly
Mozly
one more thing,
in my header I declare some constant integers which I want as global variables but keep getting the old error that it is already defined in the main obj.

Here that header:
#include <SDL.h>
#include <SDL_image.h>
#include <string>


#ifndef GLOBALFNS
#define GLOBALFNS

extern "C++"{

//declare some poiters
SDL_Surface *dot;
extern SDL_Surface *screen;



//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

SDL_Event event;

int ScreenW = 640;
int ScreenH = 480;

//total paticles
const int TOTAL_PARTICLES = 20;

//SDL supporting functions start
SDL_Surface* loadimg(std::string filename);

void applysurface(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clop);
}

#endif
Hodgman
Hodgman
use 'const static' instead of 'const'.

In this usage, static means "keep it local to this file, don't bother the linker with this variable"


Your ScreenW/ScreenH will cause trouble though -- they should be in a CPP file, and then have 'extern' versions of them in the header.
Mozly
Mozly
still one little thing,

How do you globally define a union type like SDL_Event event in a header file? Because I keep getting this error

1>main.obj : error LNK2001: unresolved external symbol "union SDL_Event event" (?event@@3TSDL_Event@@A)


even though I declared it as an external variable
smart_idiot
smart_idiot

still one little thing,

How do you globally define a union type like SDL_Event event in a header file? Because I keep getting this error

1>main.obj : error LNK2001: unresolved external symbol "union SDL_Event event" (?event@@3TSDL_Event@@A)


even though I declared it as an external variable


Hello? Hello? Anybody home? Huh? Think, McFly. Think! I gotta have time to get 'em retyped. Do you realize what would happen if I hand in my reports in your handwriting? I'll get fired. You wouldn't want that to happen, would ya? Would ya?

The union isn't special, you stick a non-extern copy of it in a source file like you did with everything else... although I'm really doubtful there's a good reason your event object needs to be seen globally.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Mozly
Mozly


.. although I'm really doubtful there's a good reason your event object needs to be seen globally.


The event handling happens in my classes which I define in other source files

But thanks I finally have this thing running
smart_idiot
smart_idiot
I fear the dependency graph for your program is going to look like a bowl of tangled spaghetti.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Mozly
Mozly
Its a relatively small program im using to learn how to make my future projects modular coz I'm tired of programs with over 9000 lines of code =).
Mozly
Mozly
Ok, So I wrote about the error on my Blog. If you read it let me know if I made any mistakes and if I can add anything.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.