2015-10-19 92 views
0

我下載了SDL2-2.0.3。我跑了./configure && make && make install。我也嘗試過brew install SDL2在OSX上安裝SDL

這是我的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 
~:. 

如何安裝呢?

回答

1

您無法鏈接libSDL2.{a,dylib}

你想:

gcc -o main main.c -lSDL2 

或者是:

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

謝謝,編譯! – quantumpotato

0

確保您下載的SDL2版本爲x64。您還需要將其與-lSDL2標誌靜態鏈接。

2

下面的命令與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) 
1

你需要運行

  • sdl2-config --cflags,這將給你的編譯標誌,並
  • sdl2-config --libs這將給你需要使用SDL2鏈接標誌

的標誌會因平臺而異這就是爲什麼你應該使用sdl2-config,而不是硬編碼一些特定的標誌到你的Makefile或其他構建腳本。