Sign in to follow this  
RaduIulianGabriel

Undefined reference to ' '

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
yckx    1298
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
pulpfist    528
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

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