Author Topic: Error Compiling 'Hello World' SDL Project from Template or from Scratch  (Read 5492 times)

Offline blipton

  • Multiple posting newcomer
  • *
  • Posts: 12
I'm using Win7x64 and have installed codeblocks 13.12.  It compiles a hello world app fine so now I'm trying to setup a similar sdl project but having issues with 2 different approaches. 

I've uncompressed SDL2-devel-2.0.3-mingw.tar.gz to a new SDL folder inside the CodeBlocks folder.   

#1
If I use the File->New Project From Template -> SDL Project and point to the root uncompressed SDL folder it complains 'can't locate the following SDL's library file: SDLmain in it'.  Sure enough I don't see that file either.
(Similar to : http://forums.codeblocks.org/index.php?topic=16546.0 )

#2
If I manually create a c++ test project and set the linker libraries to the SDL's libSDl2.a, libSDL2main.a, and SDL2.lib with the linker options ordered: -lmingw32 -lSDL2main -lSDL2 -mwindows .   In the search directories I point it to the SDL's include folder.  Finally I copied SDL2.dll to my test project's folder.

Code
#include <SDL.h>
int main()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    return 0;
}

When I compile it complains of a missing winapifamily.h.    However, I found this stackoverflow post and copied the SDL_platform.h it references and the error went away:
http://stackoverflow.com/questions/22446008/winapifamily-h-no-such-file-or-directory-when-compiling-sdl-in-codeblocks

But unfortunately, now I get 2 different errors: 
undefined reference to 'SDL_Init' and undefined reference to 'WinMain@16'
(Similar problems here : http://forums.codeblocks.org/index.php?topic=10701.0 , https://wiki.libsdl.org/FAQWindows)

I've tried everything I've read, but no luck.

I can't imagine that setting this is up can be that complicated, so I must be doing something dumb, but what?!    The SDL zip has a few include and lib folders (one in the root, one in x86_64_mingw32, and one in i686_w64_mingw32), I've tried the root ones and the x86 ones but could it be the i686?   Or is it related to the SDL_platform.h that I changed?

Does anyone have a working sdl hello world project that they wouldn't mind sharing?   Hopefully I can then reconstruct the folder paths (in case that's the issue).   Otherwise is there a way I can get the #1 approach above to work?

Thanks!

[update] I just bought the $5 ebook on code::blocks ( https://www.packtpub.com/application-development/c-application-development-codeblocks ) but fyi, it doesn't use any sdl :(
« Last Edit: December 26, 2014, 04:27:40 am by blipton »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353

Offline blipton

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Error Compiling 'Hello World' SDL Project from Template or from Scratch
« Reply #2 on: December 26, 2014, 10:24:07 pm »
Finally got approach #2 working,

1) The SDL_Platform.h file did need updating (apparently even though it's the latest 2.0.3 windows sdl2 download, it's not the latest SDL_Platform.h)

2) In the global settings, I cleared the search directories to ONLY look for the i686 inlude\SDL2 & lib paths for the compiler and linker.
(Having it look in the other (non 'i686' folders caused it to fail)

3) In the build options of the project itself, I set the linker options to '-lmingw32 -lSDL2main -lSDL2'

4) Not sure if this is actually needed but I moved the uncompressed folder out of the C: code blocks drive and onto a separate partition.   I read that could cause issues (unless you set CodeBlocks to run with administrative privileges)

At the end of the day it was pretty straightforward, even though the best approach of using the built-in sdl project template never worked. 

 I wonder if I should push my luck and see if I compile for an embedded msp430 target?!


« Last Edit: December 26, 2014, 10:28:05 pm by blipton »