找不到链接器符号

Linker symbols not found

我正在尝试用 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。错误和输出与上面相同。

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

至于更永久的解决方案,这是OSX的约定,严格来说/usr/local/lib是一个非标准的安装位置。