Partage
  • Partager sur Facebook
  • Partager sur Twitter

Multiple definition of..

erreur

Sujet résolu
    23 mai 2006 à 23:10:01

    Une erreur je crois, très courante :

    Citation : Compilateur

    multiple definition of `nom_de_fonction'


    même erreur avec une fonction initialisation qui initialise la SDL :
    int initialisation(SDL_Surface **ecran, SDL_Surface **zozor, SDL_Rect *positionZozor)
    {

        SDL_Init(SDL_INIT_VIDEO);

        *ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
        SDL_WM_SetCaption("Gestion du temps en SDL", NULL);
        *zozor = SDL_LoadBMP("zozor.bmp");
        SDL_SetColorKey(*zozor, SDL_SRCCOLORKEY, SDL_MapRGB((*zozor)->format, 255, 0, 0));

        (*positionZozor).x = (*ecran)->w / 2 - (*zozor)->w / 2;
        (*positionZozor).y = (*ecran)->h / 2 - (*zozor)->h / 2;

    }

    Je crois que mon erreur vient de cette fonction là car j'ai dans le compilateur :

    Citation : Compilateur

    .objs\fonction.o:fonction.c:(.text+0x0): multiple definition of `initialisation'
    .objs\main.o:main.c:(.text+0x0): first defined here


    ma fonction se trouve dans fonction.c
    et on fait appel a la fonction dans le main
    merci pour d'éventuelles réponses
    petite précision : je suis sous Code::Blocks (même je sais que ca n'aide pas vraiment)
    • Partager sur Facebook
    • Partager sur Twitter
      23 mai 2006 à 23:17:05

      Alors, d'aprés le compilateur, la définition de la fonction "initialisation" est présente dans ton fichier main.c ET dans fonction.c

      Vérifie que tu n'as pas déclaré deux fois cette fonction.
      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        24 mai 2006 à 11:36:24

        Citation : oussama1305

        Une erreur je crois, très courante :


        Non, seulement quand on ne sait pas bien utiliser les headers :D

        Vérifies que tu as pas inclu n'importe comment, dis nous ce que tu as fait. Normalement, c'est le prototype dans le .h, la définition dans le .c, tu inclus le .h dans le .c quand il y a besoin.
        • Partager sur Facebook
        • Partager sur Twitter
          24 mai 2006 à 14:40:46

          Mes fichiers :
          main.c
          #include <stdlib.h>
          #include <stdio.h>
          #include <SDL/SDL.h>
          #include <SDL/SDL_image.h>
          #include <SDL/SDL_ttf.h>
          #include "fonction.c"
          #include "fonction.h"


          int main(int argc, char *argv[])
          {
              SDL_Surface *zozor = NULL, *ecran = NULL;
              SDL_Rect positionZozor;
              SDL_Event event;
              int continuer = 1;
              int tempsPrecedent = 0, tempsActuel = 0;

              initialisation(&ecran, &zozor, &positionZozor);

              SDL_EnableKeyRepeat(10, 10);

              while (continuer)
              {
                  SDL_PollEvent(&event);
                  switch(event.type)
                  {
                      case SDL_QUIT:
                          continuer = 0;
                          break;
                  }
                  tempsActuel = SDL_GetTicks();
                  if (tempsActuel - tempsPrecedent > 30) /* Si 30 ms se sont écoulées */
                  {
                      positionZozor.x++; /* On bouge Zozor */
                      tempsPrecedent = tempsActuel; /* Le temps "actuel" devient le temps "precedent" pour nos futurs calculs */
                  }
                  else /* Si ça fait moins de 30ms depuis le dernier tour de boucle, on endort le programme le temps qu'il faut */
                  {
                      SDL_Delay(30 - (tempsActuel - tempsPrecedent));
                  }

                  SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
                  SDL_BlitSurface(zozor, NULL, ecran, &positionZozor);
                  SDL_Flip(ecran);
              }

              SDL_FreeSurface(zozor);
              SDL_Quit();

              return EXIT_SUCCESS;
          }



          fonction.h
          #ifndef DEF_FONCTION
          #define DEF_FONCTION

          int initialisation(SDL_Surface **ecran, SDL_Surface **zozor, SDL_Rect *positionZozor);

          #endif

          fonction.c
          #include <stdlib.h>
          #include <stdio.h>
          #include <SDL/SDL.h>
          #include <SDL/SDL_image.h>
          #include <SDL/SDL_ttf.h>
          #include "fonction.h"

          int initialisation(SDL_Surface **ecran, SDL_Surface **zozor, SDL_Rect *positionZozor)
          {

              SDL_Init(SDL_INIT_VIDEO);

              *ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
              SDL_WM_SetCaption("Gestion du temps en SDL", NULL);
              *zozor = SDL_LoadBMP("zozor.bmp");
              SDL_SetColorKey(*zozor, SDL_SRCCOLORKEY, SDL_MapRGB((*zozor)->format, 255, 0, 0));

              (*positionZozor).x = (*ecran)->w / 2 - (*zozor)->w / 2;
              (*positionZozor).y = (*ecran)->h / 2 - (*zozor)->h / 2;

          }

          Mon compilateur
          Project : SDL Application
          Compiler : GNU GCC Compiler (called directly)
          Directory : C:\Documents and Settings\Oussama\Bureau\Projects\testSDL 2\
          --------------------------------------------------------------------------------
          Switching to target: default
          Compiling: main.c
          Linking executable: testSDL 2.exe
          .objs\fonction.o:fonction.c:(.text+0x0): multiple definition of `initialisation'
          .objs\main.o:main.c:(.text+0x0): first defined here
          collect2: ld returned 1 exit status
          Process terminated with status 1 (0 minutes, 1 seconds)

          je crois que tout est en règle? :euh:
          • Partager sur Facebook
          • Partager sur Twitter
          Anonyme
            24 mai 2006 à 14:48:01

            Presque, mais tu n'as pas à inclure fonction.c dans main.c. L'inclusion copie le contenu du fichier, du coup il compile main.c contenant al fonction de fonction.c mais il compile aussi fonction.c séparément, et tu te retrouves avec deux fois la même fonction (l'originale de fonction.c et la copie incluse dans le main.c) ;)
            • Partager sur Facebook
            • Partager sur Twitter
              24 mai 2006 à 15:27:30

              Je te revaudrais ça mon vieux ;) .
              • Partager sur Facebook
              • Partager sur Twitter

              Multiple definition of..

              × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
              × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
              • Editeur
              • Markdown