Installing SDL on OSX

我怕爱的太早我们不能终老 提交于 2019-12-01 13:52:39

问题


I downloaded SDL2-2.0.3. I ran ./configure && make && make install.

I've also tried brew install SDL2.

This is my 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() );
 }
}

When I run it

~:.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
~:.

How do I install this?


回答1:


You are failing to link with libSDL2.{a,dylib}.

You want:

gcc -o main main.c -lSDL2

or perhaps:

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



回答2:


The following command is installed along with SDL and it tells you the correct switches for compiling and linking:

sdl2-config --cflags --libs

On my particular machine, that gives:

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

That means you can compile and link like this and always be assured of getting the correct settings:

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

Or, you can put it in a Makefile like this (with a TAB at the start of the second line):

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



回答3:


You need to run

  • sdl2-config --cflags which will give you the compile flags, and
  • sdl2-config --libs which will give you the linker flags

needed for using SDL2.

The flags will vary by platform which is why you should use sdl2-config instead of hardcoding some specific flags into your Makefile or other build script.




回答4:


Make sure you download the x64 SDL2 version. You also need to statically link it with -lSDL2 flag.



来源:https://stackoverflow.com/questions/33225603/installing-sdl-on-osx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!