c - 找不到链接器符号

标签 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。错误和输出与上面相同。

最佳答案

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

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

关于c - 找不到链接器符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790155/

相关文章:

c - 用于一维 double 据数组的简单 ANSI C 直方图函数?

macos - mac重新启动: previous shutdown cause: -20是什么意思

javascript - 寻找一个可以下载 HTML 和所有资源的应用程序

c++ - [UNIX] : Do I need to add all libraries in my project's makefile, 是从库中使用的,在我的项目中使用?

iphone - iPhone SDK 4 中针对多个平台

C中无法分配内存

c -/proc的内核模块

c++ - Objective-C++ 预编译头文件

python - 扩展 Python 时出现问题(链接错误)?

c++ - 赋值给整型指针地址的 Char 指针地址