找不到链接器符号

时间:2015-09-25 20:26:57

标签: c macos linker libraries undefined-symbol

我正在尝试使用gcc编译我从github获取的内容:

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)

在这种情况下,错误似乎是SDL库的一个问题,但我和其他几个我编译的小项目有着完全相同的问题。在编译之前,我安装了SDL库并检查它们是否在那里:

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

经过大量谷歌搜索后,似乎我不是唯一一个有这个问题的人,我找不到有效的解决方案。似乎osx在默认链接器路径中不包含/use/local/lib

我该如何解决这个问题(欢迎使用永久解决方案)?

修改 根据用户Alex的建议,我尝试了$ gcc -o chip8 -L/usr/local/lib chip8.c。错误和输出与上述相同。

1 个答案:

答案 0 :(得分:2)

您需要将-L/usr/local/lib -lSDL添加到您的GCC调用中。

至于更永久的解决方案,这是OS X的惯例,严格来说/usr/local/lib是非标准安装位置。