IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

Erreur de compilation "multiple definition of"


Sujet :

C++

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    78
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 78
    Points : 49
    Points
    49
    Par défaut Erreur de compilation "multiple definition of"
    Bonjour,

    je cherche à realiser un prgramme C++ qui réalise un screenshot toutes les x millisecondes.

    j'ai récupéré le code d'un "timer", mais je n'arrive pas à compiler.

    Si qqun a une idée, je suis preneur !

    Merci d'avance !

    Voici le code :

    Screenshot.cpp

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    #include <iostream>
    #include "Timer.cpp"
     
    using namespace std;
     
    int main() {
     
    	Timer timer;
    	int repetitions = 0;
     
     
    	timer.start();
     
    	while (repetitions < 100) {
     
    		if (timer.getTime() >  500) {
     
    			cout << "Répétition n°" << repetitions << endl;
    			timer.reset();
    			repetitions ++;
    		}
     
     
     
    	}//Fin boucle
     
    	timer.stop();
     
    	return 0;
    }
    Timer.cpp

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     
    #include "Timer.h"
    #include <time.h>
     
    Timer::Timer() {
    	resetted = true;
    	running = false;
    	beg = 0;
    	end = 0;
    }
     
    Timer::~Timer() {
    	// TODO Auto-generated destructor stub
    }
     
    void Timer::start() {
    	if(! running) {
    		if(resetted)
    			beg = (unsigned long) clock();
    		else
    			beg -= end - (unsigned long) clock();
    		running = true;
    		resetted = false;
    	}
    }
     
     
    void Timer::stop() {
    	if(running) {
    		end = (unsigned long) clock();
    		running = false;
    	}
    }
     
     
    void Timer::reset() {
    	bool wereRunning = running;
    	if(wereRunning)
    		stop();
    	resetted = true;
    	beg = 0;
    	end = 0;
    	if(wereRunning)
    		start();
    }
     
     
    bool Timer::isRunning() {
    	return running;
    }
     
     
    unsigned long Timer::getTime() {
    	if(running)
    		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
    	else
    		return end - beg;
    }
     
     
    bool Timer::isOver(unsigned long seconds) {
    	return seconds >= getTime();
    }
    Timer.h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    #ifndef TIMER_H_
    #define TIMER_H_
     
    class Timer {
    public:
    	Timer();
    	virtual ~Timer();
    	void start();
    	void stop();
    	void reset();
    	bool isRunning();
    	unsigned long getTime();
    	bool isOver(unsigned long seconds);
    private:
    	bool resetted;
    	bool running;
    	unsigned long beg;
    	unsigned long end;
     
    };
     
    #endif /* TIMER_H_ */
    Et Voici l'erreur que j'obtiens :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    **** Build of configuration Debug for project Screenshot ****
     
    make all 
    Building file: ../src/Screenshot.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Screenshot.d" -MT"src/Screenshot.d" -o"src/Screenshot.o" "../src/Screenshot.cpp"
    Finished building: ../src/Screenshot.cpp
     
    Building file: ../src/Timer.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Timer.d" -MT"src/Timer.d" -o"src/Timer.o" "../src/Timer.cpp"
    Finished building: ../src/Timer.cpp
     
    Building target: Screenshot
    Invoking: GCC C++ Linker
    g++  -o"Screenshot"  ./src/Screenshot.o ./src/Timer.o   
    ./src/Timer.o: In function `Timer':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:11: multiple definition of `Timer::Timer()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:11: first defined here
    ./src/Timer.o: In function `Timer':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:11: multiple definition of `Timer::Timer()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:11: first defined here
    ./src/Timer.o: In function `~Timer':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: multiple definition of `Timer::~Timer()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: first defined here
    ./src/Timer.o: In function `~Timer':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: multiple definition of `Timer::~Timer()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: first defined here
    ./src/Timer.o: In function `~Timer':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: multiple definition of `Timer::~Timer()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:18: first defined here
    ./src/Timer.o: In function `Timer::start()':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:22: multiple definition of `Timer::start()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:22: first defined here
    ./src/Timer.o: In function `Timer::stop()':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:34: multiple definition of `Timer::stop()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:34: first defined here
    ./src/Timer.o: In function `Timer::reset()':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:42: multiple definition of `Timer::reset()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:42: first defined here
    ./src/Timer.o: In function `Timer::isRunning()':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:54: multiple definition of `Timer::isRunning()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:54: first defined here
    ./src/Timer.o: In function `Timer::getTime()':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:59: multiple definition of `Timer::getTime()'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:59: first defined here
    ./src/Timer.o: In function `Timer::isOver(unsigned long)':
    /media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:67: multiple definition of `Timer::isOver(unsigned long)'
    ./src/Screenshot.o:/media/data/workspaces/WorkspaceC-CPP/Screenshot/Debug/../src/Timer.cpp:67: first defined here
    collect2: ld returned 1 exit status
    make: *** [Screenshot] Erreur 1

  2. #2
    Expert éminent sénior
    Avatar de koala01
    Homme Profil pro
    aucun
    Inscrit en
    Octobre 2004
    Messages
    11 614
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun

    Informations forums :
    Inscription : Octobre 2004
    Messages : 11 614
    Points : 30 626
    Points
    30 626
    Par défaut
    Salut,

    c'est normal, tu as inclus le fichier d'implémentation (*.cpp) dans ton fichier main...

    Or on n'inclut jamais un fichier d'implémentation; les seuls fichiers que l'on inclut étant les fichiers d'en-tête (bon, il est vrai que je simplifie un peu et que je fais l'impasse sur l'implémentation des fonctions / classes templates, mais bon )

    En effet, la directive #include est finalement très basique en soi: elle copie le contenu du fichier donné entre guillemet ou entre < > exactement à l'endroit où elle se trouve.

    Du coup, lorsque tu inclus Timer.cpp dans main.cpp, tu provoque la copie... de son contenu dans main.cpp...

    Et, comme la gestion par le préprocesseur s'effectue avant toute autre action, lorsque le compilateur prend le relais, il va générer du code pour les fonctions qui sont définies dans Timer.cpp deux fois:
    une fois lorsqu'il gérera Timer.cpp, l'autre fois quand il gérera... main.cpp...

    De ce fait, lorsque l'éditeur de liens viendra mettre la touche finale, il "perdra la boulle" et ne saura plus s'il doit effectuer la liaison avec la version qui se trouve dans le fichier objet créé au départ de main.cpp ou avec celle qui se trouve dans le fichier objet créé au départ de Timer.cpp...

    au final, la règle du "one definition rule" n'est pas respectée, et il ne te reste plus que les yeux pour pleurer
    A méditer: La solution la plus simple est toujours la moins compliquée
    Ce qui se conçoit bien s'énonce clairement, et les mots pour le dire vous viennent aisément. Nicolas Boileau
    Compiler Gcc sous windows avec MinGW
    Coder efficacement en C++ : dans les bacs le 17 février 2014
    mon tout nouveau blog

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    78
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 78
    Points : 49
    Points
    49
    Par défaut
    Merci Koala !
    En revanche, j'ai une nouvelle erreur quand je compile

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    **** Build of configuration Debug for project Screenshot ****
     
    make all 
    Building file: ../src/Screenshot.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Screenshot.d" -MT"src/Screenshot.d" -o"src/Screenshot.o" "../src/Screenshot.cpp"
    ../src/Screenshot.cpp: In function ‘int main()’:
    ../src/Screenshot.cpp:9: error: ‘Timer’ was not declared in this scope
    ../src/Screenshot.cpp:9: error: expected ‘;’ before ‘timer’
    ../src/Screenshot.cpp:13: error: ‘timer’ was not declared in this scope
    make: *** [src/Screenshot.o] Erreur 1
    Et là, je n'ai effectivement plus que mes yeux pour pleurer...

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    78
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 78
    Points : 49
    Points
    49
    Par défaut
    C'est bon, j'ai rajouté
    et ça compile !

    merci bcp !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 27
    Dernier message: 26/03/2008, 08h06
  2. problême de compilation: multiple definition
    Par Plomeg dans le forum C++
    Réponses: 2
    Dernier message: 22/11/2006, 09h17
  3. Réponses: 8
    Dernier message: 19/09/2006, 15h42
  4. [débutant] erreur "indice de liste hors limites(1)"
    Par lidouka dans le forum Langage
    Réponses: 2
    Dernier message: 13/12/2005, 14h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo