This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(35个答案)


7个月前关闭。




我正在尝试使用SDL2库进行编译,但没有成功。
OS: MacOS 10.13.2
Visual Studio Code 1.43.1
Language: c++

我正在使用Lazy Foo的非常基本的示例代码。
这是我收到的消息:
[Running] cd "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/" && g++ 01_hello_SDL.cpp -o 01_hello_SDL && "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/"01_hello_SDL
Undefined symbols for architecture x86_64:
  "_SDL_CreateWindow", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Delay", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_DestroyWindow", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_FillRect", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_GetError", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_GetWindowSurface", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Init", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_MapRGB", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_Quit", referenced from:
      _main in 01_hello_SDL-a16d96.o
  "_SDL_UpdateWindowSurface", referenced from:
      _main in 01_hello_SDL-a16d96.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

[Done] exited with code=1 in 0.168 seconds

最佳答案

构建C++代码需要三个阶段:

预处理器处理宏和#include指令,因此在此步骤中需要您的头文件。

编译器生成目标文件。这是在源文件中检查语法的时候。

链接器将目标文件和库组合为可执行文件,因此您的运行时库必须可用并指定给链接器。

对于g++,您可以通过在命令行上给出库的全名来指定库,或使用-l快捷方式来找出正确的前缀(lib)和后缀。因此,根据您的情况,我相信您可以添加-lSDL

编辑:如果不起作用,请检查输出

sdl-config --libs

关于c++ - 无法使用SDL编译:体系结构x86_64的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60818200/

10-11 22:38
查看更多