【问题标题】:Linker symbols not found未找到链接器符号
【发布时间】:2015-12-23 18:09:58
【问题描述】:

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

【问题讨论】:

  • 您需要将-lSDL 添加到您的构建命令中

标签: c macos linker libraries undefined-symbol


【解决方案1】:

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

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

【讨论】:

  • 我尝试了$ gcc -o chip8 -L/usr/local/lib chip8.c 并获得了完全相同的输出!
  • 抱歉,我忘了加-lSDL
  • 您应该学习Makefiles 或其他一些工具,例如cmake
猜你喜欢
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多