• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ChainedHollow

multiply defined symbols found

5 posts in this topic

I'm doing my first project with multiple files and I'm having trouble for some reason. I used the example for multiple source files lazyfoo.net has and while it did help ease my confusion somewhat, I am still getting errors:

1>Pong.obj : error LNK2005: "struct SDL_Surface * sScreen" (?sScreen@@3PAUSDL_Surface@@A) already defined in Functions.obj
1>Pong.obj : error LNK2005: "int const SCREEN_WIDTH" (?SCREEN_WIDTH@@3HB) already defined in Functions.obj
1>Pong.obj : error LNK2005: "int const SCREEN_HEIGHT" (?SCREEN_HEIGHT@@3HB) already defined in Functions.obj
1>Pong.obj : error LNK2005: "int const SCREEN_BPP" (?SCREEN_BPP@@3HB) already defined in Functions.obj
1>Pong.obj : error LNK2005: "struct SDL_Surface * pongSprites" (?pongSprites@@3PAUSDL_Surface@@A) already defined in Functions.obj
1>C:\Users\Brandon\Documents\Visual Studio 2010\Projects\Pong\Release\Pong.exe : fatal error LNK1169: one or more multiply defined symbols found

Pong.cpp

[CODE]
#include "Functions.h"
#include "Globals.h"
#pragma region Main
int main(int argc, char* argv[])
{

#pragma region Setup
//Quit flag
bool quit = false;
#pragma endregion Setup
#pragma region Game Logic
//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ))
{

//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

#pragma endregion Game Logic
#pragma region Rendering
SDL_FillRect(sScreen,NULL, 0x000000);
//Update Screen
SDL_Flip( sScreen );
#pragma endregion Rendering
}
return 0;
}
#pragma endregion Main
[/CODE]

Globals.h

[CODE]
#ifndef GLOBALS_H
#define GLOBALS_H
#include "SDL.h"
#include "SDL_image.h"
extern SDL_Surface* sScreen = NULL;
extern const int SCREEN_HEIGHT = 700;
extern const int SCREEN_WIDTH = 600;
extern const int SCREEN_BPP = 32;
extern SDL_Event event;
extern SDL_Surface* pongSprites = NULL;
#endif
[/CODE]

Functions.h

[CODE]
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "SDL.h"
#include "SDL_image.h"
#include <string>

bool init();
SDL_Surface *load_image( std::string filename );
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* block );
bool load_files();

#endif
[/CODE]

Functions.cpp

[CODE]
#include "Functions.h"
#include "SDL.h"
#include "SDL_image.h"
#include "Globals.h"
#include <string>

#pragma region Init

bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
sScreen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( sScreen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Pong", NULL );
//If everything initialized fine
return true;
}

#pragma endregion Init
#pragma region Load_image
//-------------------------------------------------
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
//-------------------------------------------------
#pragma endregion Load_image
#pragma region Load_Files
bool load_files()
{
pongSprites = load_image("pong.png");

if(pongSprites != NULL)
return true;
else
return false;
}
#pragma endregion Load_Files
#pragma region Apply_Surface
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* block )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, block, destination, &offset );
}
#pragma endregion Apply_Surface
[/CODE]

Thank you for any help in getting over this next obstacle, I do appreciate it.
0

Share this post


Link to post
Share on other sites
Don't do the assignment of the [i]extern[/i] variables in the header file, that turns the extern declaration into a definition and your linker is complaining about multiple definitions. Only declare them with [i]extern[/i] in the header, and define them without the [i]extern[/i] in [b]one[/b] source file.

header:
[code]
extern SDL_Surface* sScreen;
[/code]
source:
[code]
SDL_Surface* sScreen = NULL;
[/code]

You don't have to do the [i]extern[/i] declaration for primitive [i]const[/i] variables such as your integer, though. They have static linkage by default, so you can leave them in the header file but remove the [i]extern[/i] from them.
2

Share this post


Link to post
Share on other sites
[quote][code]extern SDL_Surface* sScreen = NULL;
extern const int SCREEN_HEIGHT = 700;
extern const int SCREEN_WIDTH = 600;
extern const int SCREEN_BPP = 32;
extern SDL_Event event;
extern SDL_Surface* pongSprites = NULL;[/code][/quote]

For sScreen and pngSprints (the non-const of the bunch), don't initialize them here. Instead, initialize them wherever you define them (a single .cpp file).

For SCREEN_HEIGHT, SCREEN_WIDTH and SCREEN_BPP (the const of the bunch), remove the `extern' and everything will work fine.
2

Share this post


Link to post
Share on other sites
It would be even better to wrap your global variables in functions.

For example, for your SDL_Event event object, you could do something like:
[CODE]
SDL_Event& getGlobalEvent()
{
static SDL_Event event;
retrun event;
}
[/CODE]
As it is static, it will be created only once (the first time it is called).
0

Share this post


Link to post
Share on other sites
`event' should probably not be a global variable at all: It should be a local variable in `main', and it should be passed around only to functions that might need it.
1

Share this post


Link to post
Share on other sites
Actually, none needs to be a global. You could pass event from main(), like alvaro suggested. You could do the same for pongSprites.
As for sScreen, you can remove it and use SDL_GetVideoSurface() when you need it.
0

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  
Followers 0