g++ 没有 link third-party 库

g++ does not link third-party libraries

我正在尝试使用 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

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

编辑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/ 目录中的 files/directories:

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++ third-party 库在哪里时,我给了它错误的位置 (-L/Users/myname/Documents/Code-Libraries/SDL2-2.0.5) 和一个对它毫无意义的名称 (-lSDL2)。所以我做了一些进一步的阅读,这里有一些来自 https://linux.die.net/man/1/g++:

的重要引述

You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

Linker Options

object-file-name -llibrary -nostartfiles -nodefaultlibs -nostdlib -pie -rdynamic -s -static -static-libgcc -shared -shared-libgcc -symbolic -T script -Wl,option -Xlinker option -u symbol

Directory Options

-Bprefix -Idir -iquotedir -Ldir -specs=file -I- --sysroot=dir

所以基本上,-Ldirectory 需要在 -llibrary 之前,因为 -L 选项告诉 g++ 去哪里找对于库,然后 -l 选项说明它是什么库。如果在 -l 之前没有 -L,g++ 不知道从哪里寻找 -l 的库。

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