Setting Up SDL for Eclipse on Mac OS X

I got an email from someone who was having trouble setting up SDL on Eclipse. I have never used Eclipse so I could not provide him much help. He eventually found a solution and emailed it to me so I could share it with you.

There are four things you have to do to get SDL working with Eclipse on Mac OS X in addition to what I outlined in my SDL tutorial. First, you must copy the files SDLMain.h and SDLMain.m to the same directory as your SDL program. My interpretation of this statement is if you create an Eclipse project and add your source code files to the project folder, you must also add SDLMain.h and SDLMain.m to the project folder.

Second, you must add the directory where your SDL headers reside to the Directories setting for the GCC C++ compiler. Your headers most likely reside in the following directory:

/Library/Frameworks/SDL.framework/Headers

Third, you must add linker flags for the Cocoa and SDL frameworks as well as the path to the SDLMain.m file.

-framework Cocoa
-framework SDL
/Path/To/YourProject/SDLMain.m

Fourth, you must change the following line of code in SDLMain.m:

#import “SDL.h”

to the following:

#include SDL/SDL.h

Put less than and greater than signs around the SDL/SDL.h. For some reason I can’t get those characters to show up in the blog.

Comments (15) | Trackback

15 Responses to “Setting Up SDL for Eclipse on Mac OS X”

  1. Lacroix says:

    hi, thx for that tutorial, i followed it meticulously but … its still not working :

    Invoking: MacOS X C++ Linker
    g++ -o “Osamurai” ./Combo.o ./OBB.o ./main.o -l-framework\ Cocoa -l-framework\ SDL -l/users/Timothee/Desktop/
    samurai-online/Osamurai/SDLMain.m

    /usr/bin/ld: can’t locate file for: -l-framework Cocoa

    collect2: ld returned 1 exit status
    make: *** [Osamurai] Error 1

  2. Mark says:

    I have never used Eclipse so I’m not sure how much help I can provide. But looking at the output you gave me, I can see two possible problems. First, you’re using both the -l and -framework options for the Cocoa and SDL frameworks. To link frameworks, use the -framework option, not the -l option. The -l option is for libraries, not frameworks.

    Second, you’re linking to the SDLmain.m source code file. You don’t link source code files. You just have to add the path to SDLmain.m so the compiler can find it.

    Another thing to try is to use a lowercase “cocoa” when using the -framework option.

  3. Lacroix says:

    ok thx a lot for your fast and accurate answer , I can now link Cocoa and SDL frameworks but … I still have a problem with the SDLMain.m file … I don’t know where I should write the path to the file … if you have an Idea
    Thx again

  4. Lacroix says:

    well… I think SDLmain.m is the problem since I have :

    /usr/bin/ld: Undefined symbols:
    _main

  5. Mark says:

    When using SDL your main() function has to have the following definition:

    int main(int argc, char *argv[])

    If you define main() differently (returning void instead of int, having no arguments to the main function), your SDL code won’t work because it doesn’t match the main() function in SDLMain.m.

  6. Lacroix says:

    hmmm well I’m afraid that’s exactly what my main looks like …

  7. Mark says:

    Here’s the text of the email I based the blog entry on. Maybe it will help you.

    I finally figured it out how to set SDL up for eclipse its pretty easy though:

    Copy SDLMain.m and SDLMain.h in the directory where your SDL program resides.

    These come with the SDL developers package

    In Eclipse use the following settings:

    GCC C++ compiler
    -Directories : “/System/Library/Frameworks/SDL.framework/Versions/A/Headers”

    // assuming SDL is installed in /System/Library

    Then use the following settings under the Mac OSX C++ Linker tab

    - Miscellanous – Linker flags : -framework SDL -framework Cocoa
    ~*ABSOLUTE PATH TO YOUR* SDLMain.m

  8. Lacroix says:

    thank you so much ! it’s working :)

  9. Manish says:

    Thanks so much! I was stuck with this problem for so long and was getting frustrated. This solved it!

  10. anonimo says:

    i really thanks to you, i was looking for answers y when i give up i found this, i was testing my apps in xcode and that ide is easy and little problems but i want to make portable programs (between os based on unix ,for a while) it was the reason to use eclipse, thank you.

  11. mikethebike says:

    - Miscellanous – Linker flags : -framework SDL -framework Cocoa
    ~*ABSOLUTE PATH TO YOUR* SDLMain.m

    Hi
    where exactly did u write the path to SDLMain.m? After -framework SDL does not work here.
    Thanks
    Michael

  12. Mark says:

    I don't use Eclipse so I can't answer your question. I can give you the website of the person who gave me the information I put in this post.

    http://www.eelke.com

  13. mikethebike says:

    It works!
    Compile from sources (with sdl-config) and link in the default UNIX/Linux way.
    Cheers Michael

  14. tristanbrookes says:

    Mike, you seem to have recently got this working, yet I have followed it to the letter and appear to be getting:

    make: *** No rule to make target `Main.o', needed by `AnotherTest'. AnotherTest line 0 C/C++ Problem

    Does anyone understand this because I sure don't. neither does google it seems

  15. Anonymous says:

    Thanks for the info, took a while but got it working!

    @ tristanbrookes et al that still have errors- Did you copy the SDLMain.h and .m files into the src folder of your eclipse project? You don’t need to import them or anything just copy into the folder (and make the SDLMain.h change Mark noted). Also when you add the linker flag with the absolute path, I figured out I didn’t need to start with /Users/macuser1, I just started with /Documents/…/SDLMain.m (don’t forget to include the file name too).

Leave a Reply

What is 13 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)
Powered by WordPress