我正在尝试使用SDL和C ++制作游戏。

到目前为止,我还无法做任何事情。我遵循了Mac上的所有说明(我有Mac v。10.12.2)和http://www.lazyfoo.net/tutorials/SDL/01_hello_SDL/index.php上的g ++。

目前,我遇到了链接器错误,该错误指示它们找不到-l(小写L)选项指定的库。这是我的Makefile:

CC = g++
CFLAGS = -g -Wall -std=c++11

SDL_PATH = /Users/myname/Documents/Code-Libraries/SDL2-2.0.5
SDL_INCLUDE = -I$(SDL_PATH)/include
SDL_LIB = -L$(SDL_PATH) -l/usr/local/lib/libSDL2.a

default: SDL01

SDL01: 01_hello_SDL.cpp
    $(CC) $(CFLAGS) 01_hello_SDL.cpp $(SDL_LIB) $(SDL_INCLUDE) -o SDL01

clean:
    rm -f SDL01 *.o


我已经尝试了-lSDL2。我已经尝试过-lSDL。我已经尝试了-l / usr / local / lib / libSDL2.a。我已经尝试使用-L选项(没有-l选项)以多种方式来编写/Users/myname/Documents/Code-Libraries/SDL2-2.0.5和这个(/ Users / myname / Documents / Code / Libraries /SDL2-2.0.5)给了我...

Undefined symbols for architecture x86_64:
  "_SDL_CreateWindow", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_Delay", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_DestroyWindow", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_FillRect", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_GetError", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_GetWindowSurface", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_Init", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_MapRGB", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_Quit", referenced from:
      _main in 01_hello_SDL-f65b5c.o
  "_SDL_UpdateWindowSurface", referenced from:
      _main in 01_hello_SDL-f65b5c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [SDL01] Error 1


...而其他人告诉我,他们找不到该项目的标题。我真的迷路了,很想得到一些帮助。谢谢。

编辑1:

我做了nm /usr/local/lib/libSDL2.a | grep _SDL_CreateWindow并得到了

0000000000001cb0 T _SDL_CreateWindow
0000000000000f80 T _SDL_CreateWindowAndRenderer
0000000000007620 t _SDL_CreateWindowAndRenderer_DEFAULT
                 U _SDL_CreateWindowAndRenderer_REAL
0000000000001cc0 T _SDL_CreateWindowFrom
0000000000009e80 t _SDL_CreateWindowFrom_DEFAULT
                 U _SDL_CreateWindowFrom_REAL
0000000000009e30 t _SDL_CreateWindow_DEFAULT
                 U _SDL_CreateWindow_REAL
0000000000000060 T _SDL_CreateWindowAndRenderer_REAL
                 U _SDL_CreateWindow_REAL
                 U _SDL_CreateWindow_REAL
00000000000030a0 T _SDL_CreateWindowFrom_REAL
0000000000000760 t _SDL_CreateWindowTexture
0000000000002010 T _SDL_CreateWindow_REAL
                 U _SDL_CreateWindow_REAL


编辑2:

这些是〜/ Documents / Code-Libraries / SDL2-2.0.5 /目录中的文件/目录:

Android.mk                SDL2.spec.in              cmake_uninstall.cmake.in
BUGS.txt                  TODO.txt                  configure
CMakeLists.txt            VisualC/                  configure.in
COPYING.txt               VisualC-WinRT/            debian/
CREDITS.txt               VisualC.html              docs/
INSTALL.txt               WhatsNew.txt              include/
Makefile.in               Xcode/                    sdl2-config.cmake.in
Makefile.minimal          Xcode-iOS/                sdl2-config.in
Makefile.pandora          acinclude/                sdl2.m4
Makefile.psp              android-project/          sdl2.pc.in
Makefile.wiz              autogen.sh                src/
README-SDL.txt            build/                    test/
README.txt                build-scripts/
SDL2.spec                 cmake/

最佳答案

好了,因此感谢@JohnZwinck,我得以弄清楚自己在做什么错。当告诉编译器g ++第三方库在哪里时,我给它提供了错误的位置(-L/Users/myname/Documents/Code-Libraries/SDL2-2.0.5)和一个毫无意义的名称(-lSDL2)。因此,我做了一些进一步的阅读,以下是https://linux.die.net/man/1/g++的一些重要报价:


  您可以混合使用选项和其他参数。在大多数情况下,您使用的顺序无关紧要。当您使用相同种类的多个选项时,顺序确实很重要。例如,如果您多次指定-L,则按指定的顺序搜索目录。同样,-l选项的位置也很重要。
  
  链接器选项
  
  对象文件名-llibrary -nostartfiles -nodefaultlibs -nostdlib -pie -rdynamic -s -static -static-static-libgcc -shared -shared-libgcc -symbolic -T脚本-Wl,option -Xlinker选项-u符号
  
  目录选项
  
  -B前缀-Idir -iquotedir -Ldir -specs = file -I- --sysroot = dir


因此,基本上,-L目录需要位于-llibrary之前,因为-L选项告诉g ++在哪里寻找该库,然后-l选项说明它是什么库。在-l之前没有-L的情况下,g ++不知道从-l查找库的位置。

如果我错了,请随时纠正我!但这解决了我的问题。

10-08 13:19