g ++不链接第三方库

时间:2017-02-05 01:48:08

标签: c++ macos sdl

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

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

EDIT1:

我做了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

EDIT2:

这些是〜/ 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/                    

1 个答案:

答案 0 :(得分:1)

好的,感谢@JohnZwinck,我能够弄清楚我做错了什么。当告诉编译器,g ++,第三方库所在的位置时,我给它错误的位置(-L/Users/myname/Documents/Code-Libraries/SDL2-2.0.5)和一个对它没有任何意义的名称(-lSDL2)。所以我做了一些进一步的阅读,以下是https://linux.die.net/man/1/g++的一些重要引用:

  

您可以混合选项和其他参数。在大多数情况下,您使用的订单并不重要。当您使用同一类型的多个选项时,订单确实很重要;例如,如果多次指定-L,则按指定的顺序搜索目录。此外,-l选项的位置非常重要。

     

链接器选项

     

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

     

目录选项

     

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

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

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