【问题标题】:Installing SDL on OSX在 OSX 上安装 SDL
【发布时间】:2016-01-18 11:03:03
【问题描述】:

我下载了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
~:.

如何安装?

【问题讨论】:

    标签: c++ c macos gcc sdl


    【解决方案1】:

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

    sdl2-config --cflags --libs
    

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

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      您未能与libSDL2.{a,dylib} 链接。

      你想要:

      gcc -o main main.c -lSDL2
      

      或许:

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

      【讨论】:

      • 谢谢,编译好了!
      【解决方案3】:

      你需要跑

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

      需要使用 SDL2。

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

      【讨论】:

        【解决方案4】:

        确保下载 x64 SDL2 版本。您还需要将其与-lSDL2 标志静态链接。

        【讨论】:

          猜你喜欢
          • 2023-03-12
          • 1970-01-01
          • 2013-10-25
          • 2015-01-27
          • 2014-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多