" multiple definition of `game' ", " first defined here ", " ld returned 1 exit

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 22
Reputation: Smoking Bros is an unknown quantity at this point 
Solved Threads: 0
Smoking Bros Smoking Bros is offline Offline
Newbie Poster

" multiple definition of `game' ", " first defined here ", " ld returned 1 exit

 
0
  #1
Aug 16th, 2009
Hello everyone!
The title is actually most of my compiler output!
This is my entire compiler output:
  1. multiple definition of `game'
  2. first defined here
  3. ld returned 1 exit status
  4. C:\Documents and Settings\Benjamin Dahse\Skrivebord\SDL 2D Platform Game\Makefile.win [Build Error] ["2D] Error 1
  5.  

And now this is all of my classes (The final function is that you'll be able to move around a sprite)..
Defs.h:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "SDL/SDL.h"
  5. #include "SDL/SDL_image.h"
  6.  
  7. #define SCREEN_WIDTH 640
  8. #define SCREEN_HEIGHT 450
  9.  
  10. enum
  11. {
  12. PLAYER_SPRITE,
  13. MAX_SPRITES
  14. };

Draw.cpp:
  1. #include "Draw.h"
  2.  
  3. extern void draw_player(void);
  4. extern void draw_entities(void);
  5.  
  6. void draw()
  7. {
  8. SDL_FillRect(game.screen, NULL, 0);
  9. draw_player();
  10. SDL_Flip(game.screen);
  11. SDL_Delay(1);
  12. }
  13.  
  14. void delay(unsigned int frame_limit)
  15. {
  16. unsigned int ticks = SDL_GetTicks();
  17.  
  18. if(frame_limit < ticks)
  19. {
  20. return;
  21. }
  22. if(frame_limit > ticks + 16)
  23. {
  24. SDL_Delay(16);
  25. }
  26. else
  27. {
  28. SDL_Delay(frame_limit - ticks);
  29. }
  30. }

Draw.h:
  1. #include "Structs.h"
  2.  
  3. Game game;

Graphics.cpp:
  1. #include "Graphics.h"
  2.  
  3. SDL_Surface * load_image(char * name)
  4. {
  5. SDL_Surface * temp = IMG_Load(name);
  6. SDL_Surface * image = NULL;
  7.  
  8. if(temp == NULL)
  9. {
  10. printf("Failed to load image %s\n", name);
  11. return NULL;
  12. }
  13.  
  14. SDL_SetColorKey(temp, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(temp->format, 0, 0xFF, 0xFF));
  15. image = SDL_DisplayFormat(temp);
  16. SDL_FreeSurface(temp);
  17.  
  18. if(image == NULL)
  19. {
  20. printf("Failed to convert image %s to native format\n", name);
  21. return NULL;
  22. }
  23.  
  24. return image;
  25. }
  26.  
  27. void draw_image(SDL_Surface * image, int x, int y)
  28. {
  29. SDL_Rect dest;
  30.  
  31. dest.x = x;
  32. dest.y = y;
  33. dest.w = image->w;
  34. dest.h = image->h;
  35.  
  36. SDL_BlitSurface(image, NULL, game.screen, & dest);
  37. }
  38.  
  39. void load_sprite(int index, char *name)
  40. {
  41. if(index >= MAX_SPRITES || index < 0)
  42. {
  43. printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, MAX_SPRITES);
  44. exit(1);
  45. }
  46.  
  47. sprite[index].image = load_image(name);
  48.  
  49. if (sprite[index].image == NULL)
  50. {
  51. exit(1);
  52. }
  53. }
  54.  
  55. SDL_Surface * get_sprite(int index)
  56. {
  57. if(index >= MAX_SPRITES || index < 0)
  58. {
  59. printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, MAX_SPRITES);
  60. exit(1);
  61. }
  62.  
  63. return sprite[index].image;
  64. }
  65.  
  66. void free_sprites()
  67. {
  68. int i;
  69.  
  70. for(i = 0; i < MAX_SPRITES; i++)
  71. {
  72. if(sprite[i].image != NULL)
  73. {
  74. SDL_FreeSurface(sprite[i].image);
  75. }
  76. }
  77. }
  78.  
  79. void load_all_sprites()
  80. {
  81. load_sprite(PLAYER_SPRITE, "GFX/firefly.png");
  82. }

Graphics.h:
  1. #include "Structs.h"
  2.  
  3. extern Sprites sprite[MAX_SPRITES];
  4. extern Game game;

Init.cpp:
  1. #include "Init.h"
  2.  
  3. extern void free_sprites();
  4.  
  5. void init(char * title)
  6. {
  7. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  8. {
  9. printf("Could not initialize SDL: %s\n", SDL_GetError());
  10. exit(1);
  11. }
  12.  
  13. game.screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_HWPALETTE|SDL_DOUBLEBUF);
  14.  
  15. if(game.screen == NULL)
  16. {
  17. printf("Couldn't set screen mode to %d x %d: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError());
  18. exit(1);
  19. }
  20.  
  21. SDL_WM_SetCaption(title, NULL);
  22. }
  23.  
  24. void clean_up()
  25. {
  26. free_sprites();
  27. SDL_Quit();
  28. }

Init.h:
  1. #include "Structs.h"
  2.  
  3. extern Game game;

Input.cpp:
  1. #include "Input.h"
  2.  
  3. void get_input()
  4. {
  5. SDL_Event event;
  6.  
  7. while(SDL_PollEvent(& event))
  8. {
  9. switch(event.type)
  10. {
  11. case SDL_QUIT:
  12. exit(0);
  13. break;
  14. case SDL_KEYDOWN:
  15. switch(event.key.keysym.sym)
  16. {
  17. case SDLK_UP:
  18. input.up = 1;
  19. break;
  20. case SDLK_DOWN:
  21. input.down = 1;
  22. break;
  23. case SDLK_LEFT:
  24. input.left = 1;
  25. break;
  26. case SDLK_RIGHT:
  27. input.right = 1;
  28. break;
  29. case SDLK_ESCAPE:
  30. exit(0);
  31. break;
  32. default:
  33. break;
  34. }
  35. break;
  36. case SDL_KEYUP:
  37. switch(event.key.keysym.sym)
  38. {
  39. case SDLK_UP:
  40. input.up = 0;
  41. break;
  42. case SDLK_DOWN:
  43. input.down = 0;
  44. break;
  45. case SDLK_LEFT:
  46. input.left = 0;
  47. break;
  48. case SDLK_RIGHT:
  49. input.right = 0;
  50. break;
  51. default:
  52. break;
  53. }
  54. break;
  55. }
  56. }
  57. }

Input.h:
  1. #include "Structs.h"
  2.  
  3. extern Control input;

Main.cpp:
  1. #include "Main.h"
  2.  
  3. extern void init(char *);
  4. extern void clean_up(void);
  5. extern void get_input(void);
  6. extern void draw(void);
  7. extern void init_player(void);
  8. extern void do_player(void);
  9. extern void do_entities(void);
  10. extern void load_all_sprites(void);
  11. extern void delay(unsigned int);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. unsigned int frame_limit = SDL_GetTicks() + 16;
  16. int go;
  17.  
  18. init("2D Game");
  19. atexit(clean_up);
  20. go = 1;
  21. load_all_sprites();
  22. init_player();
  23.  
  24. while(go == 1)
  25. {
  26. get_input();
  27. do_player();
  28. draw();
  29. delay(frame_limit);
  30. frame_limit = SDL_GetTicks() + 16;
  31. }
  32.  
  33. exit(0);
  34. }

Main.h:
  1. #include "Structs.h"
  2.  
  3. Game game;
  4. Control input;
  5. Entity player;
  6. Sprites sprite[MAX_SPRITES];

Player.cpp:
  1. #include "Player.h"
  2.  
  3. extern int load_sprite(char *);
  4. extern void draw_image(SDL_Surface *, int, int);
  5. extern SDL_Surface * get_sprite(int);
  6.  
  7. void init_player()
  8. {
  9. player.sprite = get_sprite(PLAYER_SPRITE);
  10. player.x = SCREEN_WIDTH / 2;
  11. player.y = SCREEN_HEIGHT / 2;
  12. }
  13.  
  14. void do_player()
  15. {
  16. if(input.up == 1)
  17. {
  18. player.y -= 3;
  19.  
  20. if(player.y < 0)
  21. {
  22. player.y = 0;
  23. }
  24. }
  25.  
  26. if(input.down == 1)
  27. {
  28. player.y += 3;
  29.  
  30. if(player.y + player.sprite->h >= SCREEN_HEIGHT)
  31. {
  32. player.y = SCREEN_HEIGHT - (player.sprite->h + 1);
  33. }
  34. }
  35.  
  36. if(input.left == 1)
  37. {
  38. player.x -= 3;
  39.  
  40. if(player.x < 0)
  41. {
  42. player.x = 0;
  43. }
  44. }
  45.  
  46. if(input.right == 1)
  47. {
  48. player.x += 3;
  49.  
  50. if(player.x + player.sprite->w >= SCREEN_WIDTH)
  51. {
  52. player.x = SCREEN_WIDTH - (player.sprite->w + 1);
  53. }
  54. }
  55. }
  56.  
  57. void draw_player()
  58. {
  59. draw_image(player.sprite, player.x, player.y);
  60. }

Player.h:
  1. #include "Structs.h"
  2.  
  3. extern Entity player;
  4. extern Control input;
  5. extern Sprites sprites[MAX_SPRITES];

and finally
Structs.h:
  1. #include "Defs.h"
  2.  
  3. typedef struct Game
  4. {
  5. SDL_Surface * screen;
  6. }Game;
  7.  
  8. typedef struct Sprites
  9. {
  10. SDL_Surface * image;
  11. }Sprites;
  12.  
  13. typedef struct Entity
  14. {
  15. int x, y;
  16. SDL_Surface * sprite;
  17. }Entity;
  18.  
  19. typedef struct Control
  20. {
  21. int up, down, left, right;
  22. }Control;

That's all of my code.. And I use Dev C++ 4.9.9.2 as a compiler..
Please help me!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: " multiple definition of `game' ", " first defined here ", " ld returned 1 exit

 
0
  #2
Aug 16th, 2009
In draw.h, you should have
extern Game game;

In game.cpp, have
Game game;
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 22
Reputation: Smoking Bros is an unknown quantity at this point 
Solved Threads: 0
Smoking Bros Smoking Bros is offline Offline
Newbie Poster

Re: " multiple definition of `game' ", " first defined here ", " ld returned 1 exi

 
0
  #3
Aug 16th, 2009
Originally Posted by Salem View Post
In draw.h, you should have
extern Game game;

In game.cpp, have
Game game;
Umm.. dude.. I don't have a game.cpp class? And if you meant Main.cpp, Game game; is not defined in there? So could you please name a class that I have? But otherwise thanks for your help
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: " multiple definition of `game' ", " first defined here ", " ld returned 1 exit

 
0
  #4
Aug 16th, 2009
Game game;
goes in ONE .cpp file.

Say draw.cpp then.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC