Sign in to follow this  

[SDL] Compile error with multiple files

This topic is 3596 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

Hi, I have three files (one header, two source). declarations.h
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

bool init();


graphics.cpp
#include "declarations.h"

bool init()
{
     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
         return false;
         
     return true;
}


main.cpp
#include "declarations.h"
using namespace std;

SDL_Surface* screen;
SDL_Surface* background;

int main(int argc, char* args[])
{
    if( init() == false )
        return 1;
        
    SDL_Quit();
}


I'm getting this error: multiple definition of 'screen' first defined here multiple definition of 'background' first defined here Id returned 1 exit status I'm using DEV-C++.

Share this post


Link to post
Share on other sites
declarations.h
#ifndef DECLARATIONS_H   // <-- header guards
#define DECLARATIONS_H

// no includes, you don't need them

bool init();

#endif // DECLARATIONS_H


graphics.cpp (would be better as declarations.cpp)
#include "SDL/SDL.h" // <-- You only need headers here
#include "SDL/SDL_image.h"

#include "declarations.h"

bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
return false;

return true;
}


main.cpp

#include ... // Whatever you need for SDL_Surface and SDL_Quit

#include "declarations.h"

SDL_Surface* screen;
SDL_Surface* background;

int main(int argc, char* args[])
{
if( init() == false )
return 1;

SDL_Quit();
}

Share this post


Link to post
Share on other sites

This topic is 3596 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.

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