7

I've been trying to get a super simple SDL program to work. I'm using Mac OS X Lion. I've got SDL to work in Snow Leopard, but it doesn't seem to want to work in lion. So far I have this:

#include <iostream>
#include "SDL/SDL.h"

using namespace std;

/*
#ifdef main
#  undef main
#endif
*/

int main( int argc, char* args[] )
{
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;
    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
    hello = SDL_LoadBMP( "hello.bmp" );
    SDL_BlitSurface( hello, NULL, screen, NULL );
    SDL_Flip( screen );
    SDL_Delay( 2000 );
    SDL_FreeSurface( hello );
    SDL_Quit();

    return 0;
}

When I try to compile this code (in Xcode 4.1) it gives me this error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
     (maybe you meant: _SDL_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I uncomment the #ifdef stuff I have commented currently, the program compiles, but then receives SIGABRT on the SDL_SetVideoMode line. That commented stuff I have I just saw in another program, I'm not sure if I'm supposed to have it or not.

How am I supposed to get this working?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cole
  • 720
  • 1
  • 8
  • 18
  • Can you provide the command issued by XCode to compile / link the program? (Get rid of that #ifdef stuff, BTW) – Brett Hale Nov 22 '11 at 02:22
  • @BrettHale I'm not sure how I'd find that command. It's using the defaults from the command line application template with C++ Edit: Think I found it: http://pastebin.com/zprm01Kp – Cole Nov 22 '11 at 02:23

4 Answers4

4

The SDL header files redefine main with a macro. This is in SDL_main.h:

#define main    SDL_main

But this is fine. SDL provides its own main() function that then calls your version. So get rid of those defines, they are making it worse, not better.

If your project is Cocoa based, then you probably missed to include SDLmain.m in your project. This provides a Cocoa friendly main() function. If your project is native C++, then my guess is that you did not include all the SDL libs in your project, so the linker isn't seeing SDL's own main().

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • My project is C++. I do see that #define line in SDL_main.h. What do you mean I didn't nuclide all the SDL libs? SDL.h, which I have included, includes a lot of other SDL header files, so I should have what I need. What else could I import? Although, I don't see any .cpp files in the framework. – Cole Nov 22 '11 at 03:22
  • SDL.h is a header file, not a library. The code from SDL is stored in a library or a framework (depending on how you installed SDL). You have to add the libraries or the framework to the project, so that the linker can get the SDL code. – Miguel Grinberg Nov 22 '11 at 03:29
  • Well, I think I did that. On my "build phases" tab of Xcode, I have SDL.framework in the "Link Binary With Libraries" section. The importing SDL.h is only in my main.cpp. Am I supposed to add the framework somewhere else? – Cole Nov 22 '11 at 03:34
  • The way I add the framework is as an entry in the project tree, along with OpenGL.framework, Foundation.framework, etc. You may want to try that, or else, install the SDL_devel package from the SDL website, then use XCode to create a new project based on the SDL template, and finally move your files to the new project. I hope this helps. – Miguel Grinberg Nov 22 '11 at 04:46
  • Sorry for being so clueless, but I don't know what the project tree is. I followed the instructions here: http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4 I would be using the template, but SDL doesn't have updated templates for Xcode 4. – Cole Nov 22 '11 at 17:32
  • Check the build log and see how XCode invokes the linking step. The command to the linker must include a "-framework SDL" option. You either added the framework to the wrong target, or some other type of mistake. – Miguel Grinberg Nov 22 '11 at 17:52
  • Hmm... It does include a -framework SDL option, along with a bunch of other stuff. I managed to get it to work by running it in the command line with `g++ -o SDL\ test main.cpp -release `sdl-config --cflags --libs`` but using `g++ -o SDL\ test main.cpp -release -framework SDL` got me the error Xcode gave me. The result of `sdl-config --cflags --libs` is `-I/opt/local/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE -L/opt/local/lib -lSDLmain -lSDL -Wl,-framework,Cocoa`. I'd still like to get this working in Xcode though. It's much nicer. – Cole Nov 23 '11 at 04:07
  • Do you have an SDL framework installed on your system? Maybe it's not there, and instead you have installed the SDL regular libraries, which clearly are installed and working. The 'sdl-config' method links against the regular SDL libs, not the SDL framework. You could add the options from the sdl-config line to your Xcode project and everything will work. If you want the XCode project to use the SDL framework then you'll have to figure out why the linker isn't finding the framework. – Miguel Grinberg Nov 23 '11 at 05:16
  • I have SDL.framework in both /Library/Frameworks, and since it's not working, I tried it in ~/Library/Frameworks, but neither worked. That's all I need to do to install the framework, right? – Cole Nov 23 '11 at 20:21
2

If you are using the SDL framework you simply need to add the files SDLMain.h and SDLMain.m to your project (assuming you already have added SDL.framework to your project).

You find these files in the "devel-lite" folder of the SDL diskimage, which you can download here: http://www.libsdl.org/release/SDL-1.2.15.dmg

These two files will give you a Cocoa-friendly main routine, so that your SDL application can be a well-behaving OS X application.

simonfi
  • 293
  • 4
  • 12
1

SDL.h does not include SDL_main.h The first line in your file should be:

#include SDL_main.h

SDL_main redefines the main function and then does its own initialization the is required to get SDL working with OS X

When compiling, you will also need to link in libSDLmain in addition to libSDL

Timulus
  • 334
  • 3
  • 12
  • That doesn't change anything for me, but I don't really understand linking libSDLmain. I'm using a framework (mac os 10.7), and in addition my SDL.h includes SDL_main.h. – Cole Jan 20 '12 at 23:39
0

I had the same link time error as OP for a pure C++ & OpenGL app, and the solutions was to use the sample project from https://github.com/Ricket/HelloSDL

This made me add the Cocoa libraries but that would have been needed anyway since I was targeting the iPhone.

Community
  • 1
  • 1
Gabor
  • 7,352
  • 4
  • 35
  • 56