Jump to content
  • Advertisement
Sign in to follow this  
RaduIulianGabriel

Undefined reference to ' '

This topic is 2524 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:

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'|



My files:
declarations.h
#ifndef DECLARATIONS_H
#define DECLARATIONS_H

#include "SDL/SDL.h"

extern SDL_Surface *screen;

extern SDL_Event event;

#endif // DECLARATIONS_H


declarations.cpp

#include "SDL/SDL.h"
#include "declarations.h"

SDL_Surface *screen = NULL;

SDL_Event event;



functions.h

#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


functions.cpp
#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();
}



constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H

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

#endif // CONSTANTS_H


main.cpp
#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;
}







Share this post


Link to post
Share on other sites
Advertisement
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!