snarehanger
snarehanger

Reputation: 231

SDL linking errors with terminal/g++ on OSX lion

I installed OSX Lion and Xcode this morning after learning C++ with Sublime Text and Terminal for a little while to use SDL but I'm having some problems when trying to compile in Terminal, here's my code.

#include "SDL/SDL.h"

int main(int argc, char* args[])
{
    //Start SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Quit SDL
    SDL_Quit();

    return 0;
}

I put SDL.framework, SDL_mixer.framework and devel-lite into my Library folder and then used Macports to get SDL when that didn't work, the latter hasn't helped.

When I use 'g++ sdltest.cpp -o sdltest -lSDL' I get the following result - 'ld: library not found for -lSDLcollect2: ld returned 1 exit status'. Is there any way I can get this working?

Also, out of interest, if I were to do a 'clean install' of SDL after formatting my harddrive and reinstalling OSX Lion and XCode (which I'm thinking about doing soon), what would be the best way to go about it? I've ran into errors with all of the guides I've found (it's definitely an issue with me then, I just need to find the easiest one to understand) and a lot of them seem to be tailored to using Xcode rather than a text editor and terminal. Thank you.

Upvotes: 1

Views: 4789

Answers (2)

Chris Doggett
Chris Doggett

Reputation: 20767

You should use the included sdl-config (or sdl2-config for SDL2) file to generate your include and library parameters to g++. For example, since I'm using SDL2 via Homebrew on OSX, this:

g++ sdltest.cpp -o sdltest `sdl2-config --cflags` `sdl2-config --lib`

converts to and runs:

g++ sdltest.cpp -o sdltest -I/usr/local/include/SDL2 -D_THREAD_SAFE -L/usr/local/lib -lSDL2

Should work the same for SDL 1.x with sdl-config, and there's additionally a "--static-libs" option, that on mine gives:

-L/usr/local/lib -lSDL2 -lm -liconv -Wl,-framework,OpenGL -Wl,-framework,ForceFeedback -lobjc -Wl,-framework,Cocoa -Wl,-framework,Carbon -Wl,-framework,IOKit -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit

Upvotes: 3

Useless
Useless

Reputation: 67802

You're telling g++ to link libSDL using -lSDL, but you don't tell it where to look. Add the SDL path to your command line with -L/Users/snarehanger/Library/SDL or whatever.


Sorry, I didn't realise this wasn't clear, so I'm incorporating Joachim's comment into the answer.

  • command-line options are case sensitive
  • option -l (lower-case ell) gives the name of a library: it doesn't specify either the filename or the search path
    • for example, -lSDL could match either libSDL.a or libSDL.so, anywhere in the search path specified on the command-line or the defaults compiled into gcc
    • gcc manual: link options
  • option -L (upper-case ell) adds a directory to the search path used when finding the libraries requested with -l
    • the full command line g++ sdltest.cpp -o sdltest -L/opt/local/lib -lSDL would allow g++ to find /opt/local/lib/libSDL.so if it exists, for example
    • gcc manual: directory options

Upvotes: 1

Related Questions