Any os x/xcode sdl gurus out there?

hi.
i hope this isnt too off topic,
Ive posted at few game sites to no avail.

after successfully making a few trivial sdl apps on win32 ive decided to try get
them working on tiger using xcode.

plain vanilla sdl works fine.
the sdl libs (sdl, sdl_image, sdl_ttf ) came in handy packages
so installation was easy. so were the sdl application templates
In fact everything built and ran with relatively little trouble

  • as long as i dont use any libraries at runtime.

I seem to have the same problem with sdl_ttf and sdl_image:
Ive included the frameworks, which allow me to build, but Im crashing
immediately at runtime. I suspect this may because of poor installation
and linking of prerequisite libs, like freetype, zlib , libpng, etc. but there
are other command line apps using these successfully so my question is
basically an xcode issue.

my procedure so far is to add frameworks with projects->add to project
and to use “search paths” for setting include and lib paths, which in the case
of the headers works for silencing compiler errors.
im more confused when things arent in the mac way of things
and im trying to add stuff from /usr/local or some odd location.
do I have to projects->add my libs as well as point to them in search paths
can i only use dylibs not .a files with osx?

if any knows any good osx sdl tutorials( that use external libs )
id be happy to take a break from the ocean of xcode documentation.
i keep thinking theres some magic bullet like setting visual studio
to multi-threaded dll but i dont know it for mac.

thanks.
tod

tod wrote:

plain vanilla sdl works fine.
the sdl libs (sdl, sdl_image, sdl_ttf ) came in handy packages
so installation was easy. so were the sdl application templates
In fact everything built and ran with relatively little trouble

  • as long as i dont use any libraries at runtime.

I have no trouble using the SDL, SDL_image, and SDL_ttf frameworks here
on Xcode 1.5. It’s been a long time since I set up the project, so I
don’t remember what I had to do manually to get it into this state and
what was done automatically, but here’s what I have:
o The three frameworks are in the Groups & Files list (in a group
"Frameworks", but I doubt that matters) and belong to the target
o Header Search Paths includes
"/Library/Frameworks/SDL.framework/Headers
$(HOME)/Library/Frameworks/SDL_image.framework/Headers
$(HOME)/Library/Frameworks/SDL_ttf.framework/Headers"
o Framework Search Paths includes "$(HOME)/Library/Frameworks"
o Library Search Paths doesn’t contain anything related to the frameworks

I seem to have the same problem with sdl_ttf and sdl_image:
Ive included the frameworks, which allow me to build, but Im crashing
immediately at runtime.

Can you use the debugger to find out where it crashes?

I suspect this may because of poor installation
and linking of prerequisite libs, like freetype, zlib , libpng, etc.

AFAIK these are compiled in statically in the frameworks.

im more confused when things arent in the mac way of things
and im trying to add stuff from /usr/local or some odd location.
do I have to projects->add my libs as well as point to them in search paths

That’s what I do, and it works, but I’m not sure whether all of it is
necessary. Now that I think of it, having the location of a library that
is in the Groups & Files in the Library Search Paths does seem
redundant. But it could be that the Library Search Paths entry was added
automatically.

can i only use dylibs not .a files with osx?

No, you can use both. But if you specify -lxyz on the compiler command
line (or Xcode does it for you) and there’s both a libxyz.a and a
libxyz.dylib around, the compiler will always prefer the dylib. To link
statically in such a case, you have to specify libxyz.a along with the
object files in the linker command line. Not sure how to do that in
Xcode, I’ve only done it on the command line.

i keep thinking theres some magic bullet like setting visual studio
to multi-threaded dll but i dont know it for mac.

Not that I know of…

-Christian

Hello tod,
Christian has a good response, so I recommend you look over it. I
thought I would supplement it with a few more details.

I seem to have the same problem with sdl_ttf and sdl_image:
Ive included the frameworks, which allow me to build, but Im crashing
immediately at runtime. I suspect this may because of poor installation
and linking of prerequisite libs, like freetype, zlib , libpng, etc. but there
are other command line apps using these successfully so my question is
basically an xcode issue.

You should really run through the debugger. If not, then at the very
least, you should at least put some fprintf(stderr,…) statements in
to see where you’re crashing. It’s possible that maybe there is a bug
in your code that other platforms let you get away with, but crashes
on OSX or PPC. The prerequisite libs you mentioned, I don’t think you
have to explicitly link against those as Chrisian pointed out.

I’m not sure if the list of libs you mentioned is the complete set you
are linking to, or if there is other stuff you haven’t mentioned. If
you are linking against any C++ libraries, remember that they must be
compiled under the same version of gcc that you are using.

my procedure so far is to add frameworks with projects->add to project
and to use “search paths” for setting include and lib paths, which in the case
of the headers works for silencing compiler errors.
im more confused when things arent in the mac way of things
and im trying to add stuff from /usr/local or some odd location.
do I have to projects->add my libs as well as point to them in search paths
can i only use dylibs not .a files with osx?

I usually use the right-click on the on of the groups (in the Groups
and Files pane) to bring me up a context menu. There is an menu item
called Add->Existing Frameworks. I’ve never tried the Projects->add to
project. They might do the same thing, but I’m not sure.

Then you must add the search paths. Select the info inspector for the
name of your project under “Targets” (in the Groups and Files) and set
the Headers Paths. We tell people to install frameworks to either
/Library/Frameworks (for system-wide) or to ~/Library/Frameworks (for
per-user). So assuming you installed to one of these locations, you
need to add these to the header paths. You can list multiple
possibilities in this field, so you might fill out the field like
this:
$(HOME)/Library/Frameworks/SDL.framework/Headers
/Library/Frameworks/SDL.framework/Headers
$(HOME)/Library/Frameworks/SDL_image.framework/Headers
/Library/Frameworks/SDL_image.framework/Headers
$(HOME)/Library/Frameworks/SDL_ttf.framework/Headers
/Library/Frameworks/SDL_ttf.framework/Headers

(all in one line, entries separated by spaces)

For other headers, you add those to the string. I don’t think
/usr/local/include is searched by default, so you would have to add
that.

This is similar for Library Search Paths. Keep in mind that Library
Search Paths don’t include Frameworks as a “Library” and they give you
a field called “Framework Search Paths” too. If you added the
frameworks in the way described above, then you shouldn’t need to add
anything to this field, but it probably won’t hurt if you explicitly
fill out these paths.

You can statically link (.a) but the system preference is to use
dynamic libraries first if they are available unless you specify
certain flags. Static linking is a pain because you must get link
order correct. I recommend you avoid it, at least for now.

But since you’re able to build, you probably already got beyond all of this.

if any knows any good osx sdl tutorials( that use external libs )
id be happy to take a break from the ocean of xcode documentation.

There are no tutorials that I know of. I’ve been kicking around the
idea of writing one, but I still need to finish cleaning up the
build/distribution system for SDL and company for OS X. And I probably
need to supplement the SDL/OS X FAQ so any tutorial is probably a bit
far off for me. So after you master all this, maybe you can write the
tutorial :slight_smile:

i keep thinking theres some magic bullet like setting visual studio
to multi-threaded dll but i dont know it for mac.

Try disabling Zero-link. That’s been the magic bullet in the past :slight_smile:
To give Apple some credit, Zerolink has started working for me
starting with Xcode 1.5. (Fix and continue is still broken for me
though.) But Zero-link still seems to cause people lots of headaches,
so maybe if you’re using it, disabling will clear up your problems.

-Eric> From: tod

disregard this if you got my first reply,
i wasnt certain if it got sent to the list,
so Im reiterating.

first, thanks christian and eric, for your prompt and informative replies.

i took a litte time learning the debugger which exposed null
pointers in my application that were causing crashes, however - the problem
is still apparently linking to dependent libs. now i can call IMG_Load ,
but it doesnt work , probably because of the lack of libpng etc.

to make things simpler , im just attempting to build "formats.c"
the demo that comes with SDL_image. if i can get this to work, im sure
the same tactics will work with
freetype and SDL_ttf , so i made a sdl application project "test_image"
and pasted formats.c into main.
both methods of adding frameworks( right click and add to project)
worked for building my project, as did adding the include path
and frameworks directory. i also noticed the sdl application template
had added -framework SDL to the linker settings so on a lark i
added -framework SDL_image.

things built but still no image support, so now for adding
the image dylibs. reading another forum , they successfully used
fink for image support, so following their lead , i add to search
directories /sw/include and /sw/lib and using christian’s suggestion
i add -lpng which will use the dylib not the .a files in /sw/lib

this produced a new set of warnings like the following:

test_image:0: warning prebinding disabled because
(__LINKEDIT segment (address =
0xd000 size = 0x8000) of
/Users/tod/test_image/
./Development/test_image.app/Contents/MacOS/test_image
overlaps with __TEXT segment (address = 0x0 size = 0x28000)
of /sw/lib/libpng.3.dylib

is this because of conflicting library versions?
or perhaps as eric suggest because the libs were
built on a different gcc? ive vaguely heard of zero link, ill find it
and shut it off im using xcode 2.1 which uses gcc 4.0 , but i
have switched back to gcc 3.3 for compatibility reasons.
i apologize for my lack of understanding, its only been a year
since i left the java sandbox. for instance if the problem
is multiple compilers, im unsure how to test which gcc was
used on a lib. i looked at man pages i thought might give me
the info, but no luck yet. ive successfully built ogre which uses
all this stuff , so i just have to keep reading so i know how to
set it up myself from scratch. tommorow im going to build
everything from source with the same compiler just to be sure.

if i get through this, id be happy to help with a tutorial or FAQ,
with of course the preface that im a novice.

thanks again for your patience and time,
tod

tod wrote:

i also noticed the sdl application template
had added -framework SDL to the linker settings so on a lark i
added -framework SDL_image.

I’m pretty sure this isn’t necessary if the framework is added to the
project (and the target). Either adding the framework or specifying
-framework xxx should be sufficient.

FWIW, here’s my Other Linker Flags: “-framework Foundation -framework
AppKit -framework OpenGL -lz -framework IOKit”. Foundation and AppKit
are the base Cocoa frameworks used by SDL, OpenGL is for OpenGL, -lz is
used by libpng in SDL_Image (so I wasn’t completely correct about
SDL_Image including everything - but zlib comes with Mac OS X, so that
shouldn’t be a problem), and I don’t know about IOKit (probably used by
SDL as well).

things built but still no image support

Care to explain in a little bit more detail? What happens? When? If I
understand you correctly, you don’t get a crash anymore? Do you get any
error message? What you say sounds quite strange to me. I’d actually
expect things to either link and work, or not link at all.

test_image:0: warning prebinding disabled because
(__LINKEDIT segment (address =
0xd000 size = 0x8000) of
/Users/tod/test_image/
…/Development/test_image.app/Contents/MacOS/test_image
overlaps with __TEXT segment (address = 0x0 size = 0x28000)
of /sw/lib/libpng.3.dylib

I don’t exactly know what those warnings mean, but I’ve learned to
ignore them. I’ve seen them in other projects too, which worked
nevertheless. Usually they don’t make the linking fail - are you sure
there isn’t an “error” (as opposed to a “warning”) anywhere?

ive vaguely heard of zero link, ill find it and shut it off

I’m using Zero Link all the time with the project I’m talking about. No
problems so far.

im using xcode 2.1 which uses gcc 4.0 , but i
have switched back to gcc 3.3 for compatibility reasons.

I still didn’t get around to installing Tiger, so it may come down to
differences between my Xcode 1.5 and your Xcode 2.1…

i apologize for my lack of understanding, its only been a year
since i left the java sandbox. for instance if the problem
is multiple compilers, im unsure how to test which gcc was
used on a lib.

AFAIK this is only a problem with C++, not with C - but I don’t know a
lot about C++. (I don’t know a lot about how static and dynamic linking
exactly works with C either, for that matter…)

-Christian

things built but still no image support

Care to explain in a little bit more detail? What happens? When? If I
understand you correctly, you don’t get a crash anymore? Do you get any
error message? What you say sounds quite strange to me. I’d actually
expect things to either link and work, or not link at all.

this is strange. at first what i meant was SDL_image doesnt
crash if you attempt to load an image format that you didnt
provide support for, ( like png which requires the additional lib)
instead IMG_Load just returns a null surface and you can deal with it.

heres a snippet from the formats.c demo:
/* load the image /
image=IMG_Load(files[i]);
if(!image)
{
/
image failed to load */
printf(“IMG_Load: %s\n”, IMG_GetError());
printf("%s is not supported, or the image data is corrupted\n", files[i+1]);
files[i+1]=NULL;
continue;
}

IMG_GetError() just prints "couldnt open "

but the formats demo comes with a test image in every format
including bmp which doesnt require any outside library so now
im not sure why at least the bmp images arent loading even though
the programs linking.i just reproduced the same directory structure as the demo,
adding the lena imagesat the same location as main. i tried adding the images to
the project as well, but i dont thinks its necessary.

im using xcode 2.1 which uses gcc 4.0 , but i
have switched back to gcc 3.3 for compatibility reasons.

I still didn’t get around to installing Tiger, so it may come down to
differences between my Xcode 1.5 and your Xcode 2.1…

im starting to wonder if this is a tiger xcode thing
so as i mentioned, im currently building everything from source on
the command line for comparison.

tod

i built SDL from source,
the latest jpg , png libs,
but had some difficulty building SDL_image because it
didnt like some syntax in jpeglib.h so i tried
fink’s SDL_image installation which succeeded.

now the same formats.c demo and the others
from SDL_image run perfectly from the command line

  • except for jpeg support which i should be able to figure out

this must be why everyone says "use the source…"
it sounds like eric was right about not being able to
use libs built with different compilers. although im
still a bit confused because he mentioned it only applying
to c++ and i thought these were all c libraries, but
im not sure.

im interested in learning more about xcode,
how to create frameworks, dylibs… so ill keep trying
to link and run there. but at least i have emacs and
the command line for the time being.

to make things simpler , im just attempting to build "formats.c"
the demo that comes with SDL_image. if i can get this to work, im sure

Good idea, but I’m not sure where this “formats.c” came from that you
speak of. The only demo I know of that comes with SDL_image is
"showimage.c". Use this one, and the projects should already be set
up for you so you don’t need to do any work.

The CVS version of SDL_image should contain a semi-updated Xcode
1.5/2.0 project that I submitted. Unpack the Xcode.tar.gz and find the
showimage folder inside. (Ignore building the SDL_image project since
you should already have it.) Open up the showimage project and try
building it. It should (hopefully) work out of the box. To test it,
you need to run it on the command line because it takes a parameter.
Do something like:
~/Desktop/SDL_image-1.2.4/Xcode/showimage.app/Contents/MacOS/showimage
yourpicutre.png

You can also try the stable source ball. There is only a PBProject
(Project Builder). You will need to update the targets to Native, and
you will need to add the /Library/Frameworks/SDL.framework/Headers to
the Header Paths. (I just tried it out.) This should all just work.

fink for image support, so following their lead , i add to search
directories /sw/include and /sw/lib and using christian’s suggestion
i add -lpng which will use the dylib not the .a files in /sw/lib

this produced a new set of warnings like the following:

test_image:0: warning prebinding disabled because
(__LINKEDIT segment (address =
0xd000 size = 0x8000) of
/Users/tod/test_image/
./Development/test_image.app/Contents/MacOS/test_image
overlaps with __TEXT segment (address = 0x0 size = 0x28000)
of /sw/lib/libpng.3.dylib

is this because of conflicting library versions?

Ugh. I think the mixing and matching of Fink and the native framework
is making things more confusing. Don’t use Fink for now. The SDL_image
framework from the website should statically link in libpng and
libjpeg so you shouldn’t even need to know anything about it. There is
no need for Fink to use SDL if you use the official frameworks from
the web site. Don’t link in libpng or libjpeg.

The command otool will show you what dependencies a library has:
otool -L /Library/Frameworks/SDL_image.framework/SDL_image

/Library/Frameworks/SDL_image.framework/SDL_image:
@executable_path/…/Frameworks/SDL_image.framework/Versions/A/SDL_image
(compatibility version 1.0.0, current version 1.0.0)
@executable_path/…/Frameworks/SDL.framework/Versions/A/SDL
(compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current
version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0,
current version 71.1.1)

Make sure your SDL_image.framework has similar output.

You can also run the tool on the application:
otool -L ~/Desktop/SDL_image-1.2.4/Xcode/showimage.app/Contents/MacOS/showimage

    /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa

(compatibility version 1.0.0, current version 11.0.0)
@executable_path/…/Frameworks/SDL_image.framework/Versions/A/SDL_image
(compatibility version 1.0.0, current version 1.0.0)
@executable_path/…/Frameworks/SDL.framework/Versions/A/SDL
(compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libmx.A.dylib (compatibility version 1.0.0, current
version 92.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0,
current version 88.0.0)

If you have additional dependencies or they go to some where (or
something) else, then something is probably wrong.

or perhaps as eric suggest because the libs were
built on a different gcc?

It is generally a C++ only issue.

ive vaguely heard of zero link, ill find it
and shut it off im using xcode 2.1 which uses gcc 4.0 , but i

Zerolink delays the loading of symbols until runtime. If you have
unresolved symbols, you don’t find out about it until you actually try
running the part of the program that needs them. Since there seems to
be a question about what happened to the library, I recommend you shut
it off. (If you select the Deployment target, zerolink is disabled by
default for that target.)

FWIW, here’s my Other Linker Flags: “-framework Foundation -framework
AppKit -framework OpenGL -lz -framework IOKit”. Foundation and AppKit
are the base Cocoa frameworks used by SDL, OpenGL is for OpenGL, -lz is
used by libpng in SDL_Image (so I wasn’t completely correct about
SDL_Image including everything - but zlib comes with Mac OS X, so that
shouldn’t be a problem), and I don’t know about IOKit (probably used by
SDL as well).

Generally, I’ve been able to use a smaller list of dependencies as the
system provided indirect dependencies seem to get pulled in
automatically. I don’t think it hurts, but I think all you really
need is:
-framework SDL_image -framework SDL -framework Cocoa
(and -framework OpenGL only if you have OpenGL code).

I still didn’t get around to installing Tiger, so it may come down to
differences between my Xcode 1.5 and your Xcode 2.1…

I’m using Tiger and Xcode 2.1. I am not having any problems.

-Eric> From: tod

By the way, this should also work:

Download the SDL_image-1.2.4 source from the website, extract the
PBProjects.tar.gz, and from in the SDL_image-1.2.4 directory:

gcc -I/Library/Frameworks/SDL.framework/Headers
-I/Library/Frameworks/SDL_image.framework/Headers showimage.c
PBProjects/showimage/SDLMain.m -framework SDL_image -framework SDL
-framework Cocoa

./a.out <your_image.png>

If this doesn’t work, I recommend you make sure you have the correct
SDL frameworks installed (perhaps remove them and reinstall), and you
don’t have any other SDL frameworks installed to /Library/Frameworks,
~/Library/Frameworks (and make sure no SDL stuff got into
/System/Library/Frameworks).

-Eric