Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 22/10/2016, à 19:08

aoilotr

[Résolu]SDL: Undefined reference to...

Bonjour, bonsoir à tous.
Avant toutes choses je tiens à m'excuser si je ne pose pas dans la bonne section du forum, j'espère qu'un modérateur sera dans le coin pour changer mon sujet de place si jamais c'est le cas.
J'ai installé la librairie SDL2 sur Ubuntu 16.04, mais je n'arrive pas à compiler correctement. Le compilateur ne trouve apparemment pas les librairies.
Voici mon message d'erreur:

blue@blue:~/Documents/Prog/C/Ochaland$ make cleanrun
rm -rf bin/*.o
make run
make[1]: Entering directory '/home/blue/Documents/Prog/C/Ochaland'
gcc -c src/main.c -w -Werror -Wall -g   -o bin/main.o 
gcc bin/main.o -w -Werror -Wall -g  -o run
bin/main.o: In function `main':
/home/blue/Documents/Prog/C/Ochaland/src/main.c:8: undefined reference to `SDL_Init'
/home/blue/Documents/Prog/C/Ochaland/src/main.c:11: undefined reference to `SDL_CreateWindow'
/home/blue/Documents/Prog/C/Ochaland/src/main.c:23: undefined reference to `SDL_GetError'
/home/blue/Documents/Prog/C/Ochaland/src/main.c:29: undefined reference to `SDL_Delay'
/home/blue/Documents/Prog/C/Ochaland/src/main.c:32: undefined reference to `SDL_DestroyWindow'
/home/blue/Documents/Prog/C/Ochaland/src/main.c:35: undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'run' failed
make[1]: *** [run] Error 1
make[1]: Leaving directory '/home/blue/Documents/Prog/C/Ochaland'
Makefile:19: recipe for target 'cleanrun' failed
make: *** [cleanrun] Error 2

L'intérieur de mon makefile:

#Constants
CC=gcc
O=bin
S=src
FLAGS=-w -Werror -Wall -g
SDL_LIB=$(sdl2-config --libs)
SDL_FLAGS=$(sdl2-config --cflags)

#Compilation
run: $(O)/main.o
	$(CC) $^ $(FLAGS) $(SDL_LIB) $(SDL_FLAGS) -o run

$(O)/main.o:
	$(CC) -c $(S)/main.c $(FLAGS) $(SDL_FLAGS) $(SDL_LIB) -o $(O)/main.o 
clean:
	rm -rf bin/*.o

cleanrun: clean
	make run

Ainsi que mon code:

#include <SDL2/SDL.h>
#include <stdio.h>

int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer

    SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

    // Check that the window was successfully created
    if (window == NULL) {
        // In the case that the window could not be made...
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    // The window is open: could enter program loop here (see SDL_PollEvent())

    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

    // Close and destroy the window
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

Je précise que j'ai installé les paquets suivants:

sudo apt-get install libsdl2-2.0
sudo apt-get install libsdl2-dev

,que la commande sdl2-config est bien installée et que dans le dossier /usr/include j'ai bien un dossier SDL2 qui contient tous les headers SDL.
J'ai cherché sur internet avant de venir ici bien évidemment mais j'ai beau changer les arguments et options des commandes de compilations en respectant un soi-disant ordre, rien y fait.
Impossible de trouver également un quelconque -libsdl ou -libsd2.
Merci de votre aide et si vous avez des questions n'hésitez pas !

Dernière modification par aoilotr (Le 22/10/2016, à 22:05)

Hors ligne

#2 Le 22/10/2016, à 22:04

aoilotr

Re : [Résolu]SDL: Undefined reference to...

Finalement avec cet agencement je parviens à compiler et à lancer le programme sans problème:

#Constants
CC=gcc
O=bin
S=src
FLAGS=-w -Werror -Wall -g
SDL_CFLAGS=`sdl2-config --cflags --libs`


#Compilation
run: $(O)/main.o
	$(CC) -o run $^ $(SDL_CFLAGS) $(FLAGS) 
$(O)/main.o:
	$(CC) -o $(O)/main.o -c $(S)/main.c $(SDL_CFLAGS) $(FLAGS)
clean:
	rm -rf bin/*.o

cleanrun: clean
	make run

Petite chose que je ne comprends pas néanmoins c'est l'utilisation des backquotes (je sais pas si c'est le bon nom) sur la ligne:

SDL_CFLAGS=`sdl2-config --cflags --libs`

Hors ligne

#3 Le 23/10/2016, à 06:39

pingouinux

Re : [Résolu]SDL: Undefined reference to...

Bonjour,

SDL_CFLAGS=`sdl2-config --cflags --libs`

Cette commande met dans la variable SDL_CFLAGS la sortie de la commande sdl2-config --cflags --libs
Tu aurais pu faire aussi

SDL_CFLAGS=$(sdl2-config --cflags --libs)

Tu peux lancer cette commande seule pour voir le résultat

sdl2-config --cflags --libs

ou

echo $SDL_CFLAGS

Ajouté : Voici un extrait de man bash

Command Substitution
       Command substitution allows the output of a command to replace the command name.   There
       are two forms:

              $(command)
       or
              `command`

Dernière modification par pingouinux (Le 23/10/2016, à 06:45)

Hors ligne