Search code examples
cmacoslinkerlibrariesundefined-symbol

Linker symbols not found


I'm trying to compile something I've fetched off of github with gcc:

users-MBP:Chip-8-Emulator user$ gcc -o chip8 chip8.c
Undefined symbols for architecture x86_64:
  "_SDL_Delay", referenced from:
      _chip8_draw in chip8-bc89fc.o
  "_SDL_Flip", referenced from:
      _chip8_draw in chip8-bc89fc.o
  "_SDL_GetKeyState", referenced from:
      _chip8_execute in chip8-bc89fc.o
      _chip8_prec in chip8-bc89fc.o
  "_SDL_GetVideoSurface", referenced from:
      _chip8_draw in chip8-bc89fc.o
  "_SDL_Init", referenced from:
      _chip8_prepare in chip8-bc89fc.o
  "_SDL_LockSurface", referenced from:
      _chip8_draw in chip8-bc89fc.o
  "_SDL_PollEvent", referenced from:
      _chip8_prepare in chip8-bc89fc.o
      _chip8_prec in chip8-bc89fc.o
  "_SDL_SetVideoMode", referenced from:
      _chip8_prepare in chip8-bc89fc.o
  "_SDL_UnlockSurface", referenced from:
      _chip8_draw in chip8-bc89fc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The error seems to be an issue with the SDL library in this case, but I've had the exact same issue with several other little projects I treid to compile. Before compiling, I installed the SDL libs and checked they are there:

users-MBP:Chip-8-Emulator user$ ls /usr/local/lib/
...           
libSDL-1.2.0.dylib       
libSDL.a                 
libSDL.dylib             
libSDLmain.a
...

After lots of Googling it seems I'm not the only one with this problem and I couldn't find solution that worked. It seems that osx doesn't include /use/local/lib in the default linker paths.

How can I fix this (a permanent solution would be welcome)?

EDIT Following a suggestion from user Alex I tried with $ gcc -o chip8 -L/usr/local/lib chip8.c. The error and output is identical to the above.


Solution

  • You'll want to add -L/usr/local/lib -lSDL to your GCC invocation.

    As for a more permanent solution, this is a convention of OS X, and strictly speaking /usr/local/lib is a non-standard installation location.