在 OSX 上安装 SDL

Installing SDL on OSX

我下载了SDL2-2.0.3。我运行./configure && make && make install.

我也试过了brew install SDL2

这是我的main.c

//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] ) {
 SDL_Window* window = NULL;
 SDL_Surface* screenSurface = NULL;

 if (SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
   printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
 }
}

当我运行它

~:.make main
gcc     main.c   -o main
Undefined symbols for architecture x86_64:
  "_SDL_GetError", referenced from:
      _main in main-d5699d.o
  "_SDL_Init", referenced from:
      _main in main-d5699d.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: *** [main] Error 1
~:.

如何安装?

确保下载 x64 SDL2 版本。您还需要使用 -lSDL2 标志静态 link 它。

您无法 link libSDL2.{a,dylib}

你想要:

gcc -o main main.c -lSDL2

或者也许:

gcc -o main main.c -L/usr/local/lib -lSDL2

以下命令与 SDL 一起安装,它告诉您编译和 linking 的正确开关:

sdl2-config --cflags --libs

在我的特定机器上,给出:

-I/usr/local/include/SDL2 -D_THREAD_SAFE
-L/usr/local/lib -lSDL2

这意味着您可以像这样编译和 link 并始终确保获得正确的设置:

g++ main.cpp -o main $(sdl2-config --cflags --libs)

或者,你可以像这样把它放在 Makefile 中(在第二行的开头有一个 TAB):

main:   main.cpp
        g++ main.cpp -o main $$(sdl2-config --cflags --libs)

你需要运行

  • sdl2-config --cflags 会给你编译标志,
  • sdl2-config --libs 这将为您提供链接器标志

需要使用 SDL2。

标志会因平台而异,这就是为什么您应该使用 sdl2-config 而不是将某些特定标志硬编码到您的 Makefile 或其他构建脚本中的原因。