11 Replies - 539 Views - Last Post: 07 September 2012 - 03:00 PM Rate Topic: -----

#1 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

C++ SDL Makefile Issue

Posted 07 September 2012 - 01:21 PM

I've been trying to build this code for hours and have no idea what's going on, any help would be appreciated. Here's the code:

main.cpp:
#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"

int main(int argc, char** args) 
{
	if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
	{
		SDL_Quit();
	}
	
	while(core.desiredstate != core.quit)
	{
		::currentstate->EventHandling();
		::currentstate->Logic();
		core.ChangeState();
		::currentstate->Render();
		::currentstate->Update();
	}
	
	SDL_FreeSurface(core.screen);
	SDL_Quit();
}



Core.h:

#ifndef CORE_H
#define CORE_H
#include "SDL/SDL.h"
#include <string>

class Core
{
	public:
		SDL_Surface* Load(std::string filename);
		SDL_Surface* screen;
		
		bool Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS);
		
		void ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
		void SetState(int newstate);
		void ChangeState();
		
		enum state { intro, state_null, quit };
		
		int desiredstate, stateID;
} core;

#endif



Core.cpp:

#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>

/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS) 
{
	//starts SDL subsystems, returns false upon error
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return false;
	}
	
	//The screen
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
	//Returns false if there was an error
	if(screen == NULL)
	{
		return false;
	}
	
	SDL_WM_SetCaption("Game", NULL);
	
	return true;
}

/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
	//original loaded image
	SDL_Surface* original = SDL_LoadBMP(filename.c_str());
	SDL_Surface* optimized = NULL;
	
	if(original != NULL)
	{
		//Sets optimized to optimized version of original
		optimized = SDL_DisplayFormat(original);
		
		SDL_FreeSurface(original);
	}
	
	return optimized;
}

/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{
	//holds the x y coordinates
	SDL_Rect location;
	location.x = x;
	location.y = y;
	
	if(destination != NULL) 
	{
		SDL_BlitSurface(source, NULL, destination, &location);
	}
}

/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate) 
{
	if(desiredstate != state_null && desiredstate != quit)
	{
		desiredstate = newstate;
	}
}

/* Changes the game state */
void Core::ChangeState()
{
	if(desiredstate != state_null && desiredstate != quit)
	{
		//frees old state memory
		delete ::currentstate;
		
		switch(desiredstate)
		{
			case intro:
				//allocates new state memory
				::currentstate = new Intro();
			break;
		}
		
		stateID = desiredstate;
		desiredstate = state_null;
	}
}



GameStates.h:

#ifndef GAMESTATES_H
#define GAMESTATES_H

class GameStates 
{
	public:
		virtual void EventHandling() = 0;
		virtual void Logic() = 0;
		virtual void Render() = 0;
		virtual void Update() = 0;
};

#endif



Intro.h

#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"

class Intro : public GameStates
{
	private:
		SDL_Surface* test;
	public:
		Intro();
		void EventHandling();
		void Logic();
		void Render();
		void Update();
		~Intro();
};

#endif



Intro.cpp

#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"

Intro::Intro()
{
	test = core.Load("test.bmp");
}

void Intro::EventHandling()
{
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
			case SDL_QUIT:
				core.SetState(core.quit);
			break;
		}
	}
}

void Intro::Logic()
{
	//shit
}
void Intro::Render()
{
	core.ApplySurface(30, 30, test, core.screen);
}

void Intro::Update()
{
	SDL_Flip(core.screen);
}

Intro::~Intro()
{
	SDL_FreeSurface(test);
}



globals.h

#ifndef GLOBALS_H
#define GLOBALS_H
#include "GameStates.h"
#include "SDL/SDL.h"

extern GameStates* currentstate;

#endif;



globals.cpp

#include "globals.h"
#include "GameStates.h"

currentstate = NULL;



Makefile:

BJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL

game : $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)

main.o : Core.h GameStates.h globals.h 
	$(CC) -c $(CFLAGS) main.cpp

Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
	$(CC) -c $(CFLAGS) Core.cpp

Intro.o : Intro.cpp GameStates.h Core.h
	$(CC) -c $(CFLAGS) Intro.cpp



And the error it gives:

g++ -Wall -g  -o game -lSDL
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [game] Error 1



Any help would be very much appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ SDL Makefile Issue

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 01:32 PM

BJS = main.o Intro.o Core.o


Something missing from the beginning of this line?

Look at your makefile command:

$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)


and the variables contained therein:

CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL


then compare with what was run:

g++ -Wall -g  -o game -lSDL


and it's pretty obvious there's something missing there.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 01:37 PM

In your makefile, you forgot to set the OBJS macro to contain all your .o files.

Ah. JackOfAllTrades beat me to it. :)

I suspect our poster got bit by using 'vi' as his editor. I'm quite sure he pressed 'O', but the editor just didn't do quite what he expected.
Was This Post Helpful? 1
  • +
  • -

#4 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 01:55 PM

Yeah, that's exactly what happened within vi. But, now upon using make, I get this error:

/home/lincoln/Desktop/test/src/Intro.cpp:6: multiple definition of `core'
main.o:/home/lincoln/Desktop/test/src/main.cpp:7: first defined here
Core.o: In function `Core::Initilization(int, int, int, int)':
/home/lincoln/Desktop/test/src/Core.cpp:10: multiple definition of `core'
main.o:/home/lincoln/Desktop/test/src/main.cpp:7: first defined here
main.o: In function `main':
main.cpp:(.text+0x49): undefined reference to `currentstate'
main.cpp:(.text+0x56): undefined reference to `currentstate'
main.cpp:(.text+0x62): undefined reference to `currentstate'
main.cpp:(.text+0x73): undefined reference to `currentstate'
main.cpp:(.text+0x89): undefined reference to `currentstate'
main.o:main.cpp:(.text+0x9a): more undefined references to `currentstate' follow
collect2: ld returned 1 exit status
make: *** [game] Error 1


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 01:59 PM

Look at line 21 of Core.h. Each file that includes Core.h will have a global Core core defined.

If you want it to be global, probably declare in in Globals.h, and define it in Globals.cpp.
Was This Post Helpful? 0
  • +
  • -

#6 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:03 PM

I've fixed that, thank you, but what about the undefined references?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:10 PM

I'm not seeing globals.o being built or linked into your makefile.

A minor suggestion, if you don't mind being outside the mainstream. Yes, most people use make, but consider looking at jam. For simple cases, you just list your target and list of sources and it takes care of doing all the dependencies for you.

http://www.perforce....cumentation/jam
http://www.freetype.org/jam/index.html
Was This Post Helpful? 0
  • +
  • -

#8 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:17 PM

Added that and got the same error, it's got to be buried somewhere in my crappy code. Sorry to bother with this, I've just been stuck for a while now. I really appreciate the help.

Woops, that fixed the undefined reference, turned out I forgot to add it to the OBJS macro. It works. Lovely. Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:23 PM

Crap... Now I get a segfault...
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:28 PM

It doesn't look like you initialize currentstate, but I'm just scanning the code. Of course, you would have hard confirmation of that if you ran your code through a debugger.
Was This Post Helpful? 0
  • +
  • -

#11 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 02:37 PM

This is what I got from running it through gdb:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400e61 in main (argc=1, args=0x7fffffffe1f8) at main.cpp:17
warning: Source file is more recent than executable.
17		while(core.desiredstate != core.quit)



Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3162
  • View blog
  • Posts: 9,548
  • Joined: 05-May 12

Re: C++ SDL Makefile Issue

Posted 07 September 2012 - 03:00 PM

I would try rebuilding to make the binaries/symbols match up with the source code. I suspect that you'll find the crash where you first try to reference the currentstate pointer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1