• Advertisement
Sign in to follow this  

Undefined reference to ' '

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It's the first time I'm trying to make something using multiple source files. When compiling I keep getting this error:

[code]D:\C++ Universe\Snake\.objs\main.o:main.cpp|| undefined reference to `screen'|
D:\C++ Universe\Snake\.objs\main.o:main.cpp|| undefined reference to `event'|
D:\C++ Universe\Snake\.objs\main.o:main.cpp|| undefined reference to `event'|
D:\C++ Universe\Snake\.objs\functions.o:functions.cpp|| undefined reference to `screen'|
[/code]


My files:
declarations.h
[code]#ifndef DECLARATIONS_H
#define DECLARATIONS_H

#include "SDL/SDL.h"

extern SDL_Surface *screen;

extern SDL_Event event;

#endif // DECLARATIONS_H
[/code]

declarations.cpp

[code]#include "SDL/SDL.h"
#include "declarations.h"

SDL_Surface *screen = NULL;

SDL_Event event;

[/code]

functions.h

[code]#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include "SDL/SDL.h"
#include <string>

SDL_Surface *load_image (std::string filename);
void draw (int x, int y, SDL_Surface *source, SDL_Surface *destination);
void clean_up();


#endif // FUNCTIONS_H
[/code]

functions.cpp
[code]#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "declarations.h"
#include "functions.h"
#include <string>

SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;

loadedImage = IMG_Load(("data/images" + filename).c_str());

if (loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface (loadedImage);

if (optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
}
}

return optimizedImage;
}

bool load_files ()
{
return true;
}

void draw (int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;

SDL_BlitSurface (source, clip, destination, &offset);
}

void clean_up()
{
SDL_FreeSurface(screen);

SDL_Quit();
}

[/code]

constants.h
[code]#ifndef CONSTANTS_H
#define CONSTANTS_H

const int screen_width = 800;
const int screen_height = 600;

#endif // CONSTANTS_H
[/code]

main.cpp
[code]#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "declarations.h"
#include "functions.h"
#include "constants.h"

int main (int argc, char* args[])
{
bool running = true;
Uint32 time;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(screen_width, screen_height, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Snake", NULL);

while (running)
{
time = SDL_GetTicks();
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
running = false;
}
}
if (1000/30 > (SDL_GetTicks() - time))
{
SDL_Delay((1000/30) - (SDL_GetTicks() - time));
}
}

clean_up();
return 0;
}

[/code]





Share this post


Link to post
Share on other sites
Advertisement
I'm not sure you really grok how code organization across multiple files works. Have you read [url="http://www.gamedev.net/page/resources/_/reference/programming/languages/c-and-c/organizing-code-files-in-c-and-c-r1798"]Organizing Code Files in C and C++[/url]?

Share this post


Link to post
Share on other sites
The code looks good enough.
My best guess is that the linker is not provided with all the translation units.
If you are using Visual Studio make sure you add all the .cpp files to your project.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement