2017-05-12 3 views
0

J'essaye d'obtenir la bibliothèque de SDL fonctionnant sur mon macbook pour écrire un petit jeu (probablement tetris) dans c. J'ai lu et suivi les instructions d'installation pour SDL au https://wiki.libsdl.org/Installation.C ne compilera pas avec sdl.h inclus

Cependant, quand j'ai essayé de compiler le code C suivant:

#include <SDL2/SDL.h> 
#include <stdio.h> 
int main() 
{ 
    SDL_Init(SDL_INIT_VIDEO); 
    printf("Window Initialization!\n"); 
    SDL_Window *window; 
    //printf("SDL_WINDOWPOS_CENTERED is %d\n", SDL_WINDOWPOS_CENTERED); 
    window = SDL_CreateWindow(
          "SDL2 Test", 
           SDL_WINDOWPOS_CENTERED, 
           SDL_WINDOWPOS_CENTERED, 
           640, 480, 0); 
    if(window == NULL){ 
     printf("Creation of Window Failed\n"); 
     SDL_Quit(); 
     return -1; 
    } 
    SDL_Delay(3000); //delay for 3000 ms 
    SDL_Renderer* rend = NULL; 
    SDL_DestroyWindow(window); 
    SDL_Quit(); 
    return 0; 
} 

avec la commande:

gcc main.c -o run 

L'erreur suivante est imprimé:

Undefined symbols for architecture x86_64: 
"_SDL_CreateWindow", referenced from: 
    _main in sdl1-a80c27.o 
"_SDL_Delay", referenced from: 
    _main in sdl1-a80c27.o 
"_SDL_DestroyWindow", referenced from: 
    _main in sdl1-a80c27.o 
"_SDL_Init", referenced from: 
    _main in sdl1-a80c27.o 
"_SDL_Quit", referenced from: 
    _main in sdl1-a80c27.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Depuis que je l'ai fait make et sudo make install, selon le site, le SDL devrait être "universel" maintenant. Donc je suppose que je n'aurais pas besoin de linker. Mais apparemment, ce n'est pas le cas.

Auparavant, j'ai également essayé d'ajouter simplement le fichier de structure dans/Bibliothèque/Frameworks. Et puis si je l'ai fait:

gcc sdl1.c -o run -F/Library/Frameworks/SDL2.framework/Headers -framework SDL2 

Le code peut être compilé. Cependant, parfois je veux aussi inclure SDL2_image.h et ajouter un autre -F /.../ ne fonctionnera pas.

J'apprécierais vraiment des suggestions/conseils. Merci!

+1

Vous devez probablement compiler avec '-lSDL'. – immibis

+0

Pour obtenir le drapeau, utilisez 'sdl2-config --cflags --libs' –

Répondre

5

De la documentation

gcc sdl1.c -o run `sdl2-config --cflags --libs` 

sdl est livré avec un outil pour générer pour vous tous les drapeaux, il suffit d'utiliser, il est la meilleure façon de maintenir votre système de construction.