I keep getting an error when compiling an SDL program I'm working on.

I get the "'screen' was not declared in this scope" error, even though I'm pretty sure it's declared in 'globals.h'.

globals.h

#ifndef GLOBALS_H_INCLUDED
#define GLOBALS_H_INCLUDED
#include "gui.h"
#include "player.h"
#include "SDL.h"
#include "timer.h"
#include <stdlib.h>
#include <string>

SDL_Surface* screen = NULL;
SDL_Event event;
bool done;
int screenW = 640, screenH = 480

#endif

setup.h

#ifndef SETUP_H_INCLUDED
#define SETUP_H_INCLUDED
#include "globals.h"
#include "SDL.h"
#include "SDL_ttf.h"
#include <windows.h>

int createWindow()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    TTF_Init();

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_WM_SetCaption( "Thing", NULL );

    screen = SDL_SetVideoMode(screenW, screenH, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_HWACCEL/*|SDL_RESIZABLE*/);
    if(!screen)
    {
        printf("Unable to set %ix%i video: %s\n", screenW, screenH, SDL_GetError());
        return 1;
    }

    return 0;
}

#endif

I get the error in the createWindow() function. It also says 'screenW' and 'screenH' aren't declared. Why is this? global.h is included and it worked all the time up until now.

Recommended Answers

I think you need to prepend the keyword "extern" in line 10 of globals.h like so:

extern SDL_Surface* screen = NULL;

I also think that you need to prepend "extern" before the declaration of all the other global variables in globals.h.

Jump to Post

All 3 Replies

anyone?

This forum is really helpful...

I think you need to prepend the keyword "extern" in line 10 of globals.h like so:

extern SDL_Surface* screen = NULL;

I also think that you need to prepend "extern" before the declaration of all the other global variables in globals.h.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.