Allegro Linker Error

Started by
3 comments, last by lukeymoo 12 years, 8 months ago
[font=arial, verdana, tahoma, sans-serif][size=2]Hey everyone, I've tried searching around multiple forums and sites in search of an answer but none seem to work.
I have a header file that holds global BITMAP variables and I've included them in 2 CPP files, that is the gameEngine.cpp and Main.cpp[/font][font=arial, verdana, tahoma, sans-serif][size=2]I've posted this before, but I guess I did so in the wrong forum as I got no replies.

Here is my Bitmaps.h file
[source lang='cpp']
#ifndef BITMAPS_H
#define BITMAPS_H
#include

extern BITMAP *buffer;
extern BITMAP *p_right;
extern BITMAP *p_left;
extern BITMAP *p_jump_left;
extern BITMAP *p_jump_right;[source='gameEngine.cpp']if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) < 0) { MessageBox(NULL, "Allegro failed to initialize the graphics!", "Error!", MB_OK | MB_ICONEXCLAMATION); exit(1); } set_window_title("TITLE REMOVED"); BITMAP *buffer = create_bitmap(640, 480); BITMAP *p_right = load_bitmap("gfx\\p_right.bmp", NULL); BITMAP *p_left = load_bitmap("gfx\\p_left.bmp", NULL); BITMAP *p_jump_left = load_bitmap("gfx\\p_jump_left.bmp", NULL); BITMAP *p_jump_right = load_bitmap("gfx\\p_jump_right.bmp", NULL); BITMAP *menu_background = load_bitmap("data\\menu_background.bmp", NULL); BITMAP *menu_play = load_bitmap("data\\play.bmp", NULL); BITMAP *menu_info = load_bitmap("data\\info.bmp", NULL); BITMAP *menu_exit = load_bitmap("data\\exit.bmp", NULL);[/source]

extern BITMAP *menu_background;
extern BITMAP *menu_play;
extern BITMAP *menu_info;
extern BITMAP *menu_exit;
#endif[/source]
[font="arial, verdana, tahoma, sans-serif"]Sorry for long post,

And here is a snippet of my gameEngine.cpp in the 'init()' function
[source lang="cpp"]
set_window_title("Halo: The Untold");
BITMAP *buffer = create_bitmap(640, 480);
BITMAP *p_right = load_bitmap("gfx\\p_right.bmp", NULL);
BITMAP *p_left = load_bitmap("gfx\\p_left.bmp", NULL);
BITMAP *p_jump_left = load_bitmap("gfx\\p_jump_left.bmp", NULL);
BITMAP *p_jump_right = load_bitmap("gfx\\p_jump_right.bmp", NULL);
BITMAP *menu_background = load_bitmap("data\\menu_background.bmp", NULL);
BITMAP *menu_play = load_bitmap("data\\play.bmp", NULL);
BITMAP *menu_info = load_bitmap("data\\info.bmp", NULL);
BITMAP *menu_exit = load_bitmap("data\\exit.bmp", NULL);
show_mouse(screen);
[/source]
And my main.cpp's use of these BITMAPs
[source lang="cpp"]
switch(g.state)
{
case MENUSCREEN:
//making menuscreen background
draw_sprite(buffer, menu_background, 0, 0);
masked_blit(menu_play, buffer, 0, 0, 300, 300, 189, 50);
masked_blit(menu_info, buffer, 0, 0, 300, 350, 189, 50);
masked_blit(menu_exit, buffer, 0, 0, 300, 400, 189, 50);
/*play = 300, 300 info = 300, 350 exit = 300, 400*/
break;
case GAME:
break;
case CREDITS:
break;
}
[/source]
Here are the list of confusing linker errors
<div>Linking...
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_exit" (?menu_exit@@3PAUBITMAP@@A)
Main.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_exit" (?menu_exit@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_info" (?menu_info@@3PAUBITMAP@@A)
Main.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_info" (?menu_info@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_play" (?menu_play@@3PAUBITMAP@@A)
Main.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_play" (?menu_play@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_background" (?menu_background@@3PAUBITMAP@@A)
Main.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_background" (?menu_background@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * p_jump_right" (?p_jump_right@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * p_jump_left" (?p_jump_left@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * p_right" (?p_right@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * p_left" (?p_left@@3PAUBITMAP@@A)
gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * buffer" (?buffer@@3PAUBITMAP@@A)
Main.obj : error LNK2001: unresolved external symbol "struct BITMAP * buffer" (?buffer@@3PAUBITMAP@@A)

Thanks ahead for reply :][/font][/font]
Advertisement
So take the first error, for example:

> gameEngine.obj : error LNK2001: unresolved external symbol "struct BITMAP * menu_exit" (?menu_exit@@3PAUBITMAP@@A)

You've used menu_exit in your code, but the linker can't actually resolve that to a variable. I.e., it can't find the definition. So...you have to ask yourself, where is the definition?
Well the definition is in the gameEngine.cpp, which I don't think can be used can it ? But even if it couldn't I've also tried placing the definitions in different places such as in Main.cpp and Even in the Bitmaps.h and it'd throw the same errors at me..I've also tried using it without extern. I am just really really stumped right now.
> Well the definition is in the gameEngine.cpp

At global scope? "And here is a snippet of my gameEngine.cpp in the 'init()' function" So all those definitions are at function scope, which doesn't correspond to the global declarations.

> But even if it couldn't I've also tried placing the definitions in different places such as in Main.cpp and Even in the Bitmaps.h

The definition goes in a cpp file. Not in a header file.

> .I've also tried using it without extern.

Turning your declaration in a header file into a definition. Not correct.
I've taken your advice, and put the definitions in global scope of gameEngine.cpp and It compiles correctly but now it's giving me access violations on run-time for each of the Bitmaps, had this problem before, but this one seems to need a different solution.

This topic is closed to new replies.

Advertisement