Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

macos - Setting up SDL on Mac OS X Lion with Xcode 4

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...