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 :

Problème de compilation séparée


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 36
    Par défaut Problème de compilation séparée
    Bonjour,
    bon voila je commence à m'arracher les cheveux

    Mon projet de code se compose de 3 fichiers principaux pour l'instant

    main.c
    cards.c
    cards.h

    main.c :
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "cards.h"
     
     
     
     
    int main(void)
    {
    	struct cardgame *newgame ;
    	struct player *newplayer ;
    	Init_Sorted_CardGame(newgame) ;
    	Set_Shuffled_CardGame(newgame) ;
    	Deal_To_Player_Cards(newgame,newplayer,5);
    	system("pause") ;
    	return 0 ;
     
    }
    Début du cards.h :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    #ifndef CARDS_H
    #define CARDS_H
    ... mes prototypes ...
    #endif
    Début du cards.c :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "cards.h"
    Erreur persistante de compilation :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Linking console executable: D:\blackjack\blackjack.exe
    .objs\tmp\cards.o:cards.c:(.data+0x0): multiple definition of `ColorTable'
    .objs\tmp\main.o:main.c:(.data+0x0): first defined here
    .objs\tmp\cards.o:cards.c:(.data+0x40): multiple definition of `ValueTable'
    .objs\tmp\main.o:main.c:(.data+0x40): first defined here
    collect2: ld returned 1 exit status

    Je pige vraiment pas.. mon code marchait pourtant bien avant que je rajoute de nouvelles fonctions proprement..

    Merci pour l'aide.

  2. #2
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Ben là on a besoin du code de main.c et cards.c parce que apparemment tu définie 2 fois quelque chose.

  3. #3
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 36
    Par défaut
    main.c
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "cards.h"
     
     
     On_Malloc_Error(){
    	printf("Malloc error !\n");
    	system("pause");
    	exit(0);
    }
     
     
    int main(void)
    {
    	struct cardgame *newgame ;
    	struct player *newplayer ;
    	Init_Sorted_CardGame(newgame) ;
    	Set_Shuffled_CardGame(newgame) ;
    	Deal_To_Player_Cards(newgame,newplayer,5);
    	system("pause") ;
    	return 0 ;
     
    }
    cards.c :

    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "cards.h"
     
    /**
    *\brief Free the memory allocated for a CardLinkList struct
    *\param struct CardLinkedListEntryPoints* EntryPoints EntryPoints to the double linked list to be freed.
    */
     
    void CardLinkedList_Clear(struct CardLinkedListEntryPoints* EntryPoints){
     
        struct CardLinkedList* tmptofree ;
        struct CardLinkedList* tmpbrowse ;
     
        // initialisation of tmpbrowse with the pointer towards the first element of the double linked list to free
     
        tmpbrowse = EntryPoints->first ;
     
        // We loop as long as we're not at the last element of the d l l (double linked list)
     
        while(tmpbrowse != NULL){
     
            //init of tmptofree with the pointer to the current item of the doublelinkedlist
     
            tmptofree = tmpbrowse;
     
            // init tmpbrowse with the pointer to the next value
     
            tmpbrowse = tmpbrowse->next ;
     
            // We free the current element
     
            free(tmptofree) ;
            }
     
            // clean init of EntryPoint struct
     
            EntryPoints->first = NULL;
            EntryPoints->last = NULL;
     
    }
     
     
     
    /**
    *\brief initialize a doublelinked list
    *\param struct CardLinkedListEntryPoints* EntryPoints ; struct containing pointers towards first and last element.
    */
    void Init_CardLinkedList(struct CardLinkedListEntryPoints* EntryPoints){
    	EntryPoints->first = NULL;
    	EntryPoints->last  = NULL;
    }
     
    /**
    *\brief CardLinkedList_Set_Top_Card insert a card at the beginning of a double linked struct containing cards
    *\param struct card CardToAdd The card to insert
    *\param struct CardLinkedListEntryPoints* EntryPoints struct containing entry points for double linked struct containing cards
    */
     
     
    void CardLinkedList_Set_Top_Card(struct card CardToAdd,struct CardLinkedListEntryPoints* EntryPoints){
     
    	//Memory allocation for item to be inserted ; return a pointer to the item
     
    	struct CardLinkedList* NewItem = malloc(sizeof(struct CardLinkedList));
     
    	//Test if memory allocation was a success
     
    	if (NewItem == NULL) On_Malloc_Error() ;
     
    	// Initialisation of the newitem with the card given in parameters
     
    	strcpy(NewItem->current.color,CardToAdd.color);
    	strcpy(NewItem->current.value,CardToAdd.value);
     
    	// We insert this new item in top position so we link it to the old first
     
    	NewItem->next = EntryPoints->first;
     
    	// newitem is the first one , NULL indicate that there's no item before this one
     
    	NewItem->prev = NULL;
     
    	// Update of entrypoints structs ; the first item pointed is now our new item
     
    	EntryPoints->first = NewItem;
     
    }
     
    /**
    *\brief CardLinkedList_Set_Last_Card insert a card at the end of a double linked struct containing cards
    *\param struct card CardToAdd The card to insert
    *\param struct CardLinkedListEntryPoints* EntryPoints struct containing entry points for double linked struct containing cards
    */
     
    void CardLinkedList_Set_Last_Card(struct card CardToAdd, struct CardLinkedListEntryPoints* EntryPoints){
     
     
    	//Memory allocation for item to be inserted ; return a pointer to the item
     
    	struct CardLinkedList* NewItem = malloc(sizeof(struct CardLinkedList));
     
    	//Test if memory allocation was a success
     
    	if (NewItem == NULL) On_Malloc_Error() ;
     
    	// Initialisation of the newitem with the card given in parameters
     
    	strcpy(NewItem->current.color,CardToAdd.color);
    	strcpy(NewItem->current.value,CardToAdd.value);
     
    	//NewItem is the last card,  Null indicate that there's no item after
    	NewItem->next = NULL;
     
    	//We link the new last item with the old last one
     
    	NewItem->prev = EntryPoints->last;
     
    	//Update of the entrypoint struct to indicate last item is pointed by newitem pointer
     
    	EntryPoints->last = NewItem;
    }
     
     
    /**
    *\brief count number of cards element in d l l EntryPoints
    *\param struct CardLinkedListEntryPoints* EntryPoints
    *\return int count number of elements
    */
     
     
    int CardLinkedList_Count_Elements(struct CardLinkedListEntryPoints* EntryPoints){
     
        int count = 0;
     
        struct CardLinkedList *tmp;
     
        tmp = EntryPoints->first;
     
        while(tmp != NULL){
     
            count++;
     
            tmp = tmp->next;
        }
     
        return count ;
    }
     
     
     
    /**
    *\brief init a struct cardgame with sorted card struct
    *\param struct cardgame newgame
    */
     
    void Init_Sorted_CardGame(struct cardgame * newgame){
    	int i,j,k ;
    	i = 0;
    	char TempValue[10];
    	char TempColor[6];
    	for(j=0;j<4;j++)
    				{
    					for(k=0;k<13;k++)
    						{
    							strcpy( newgame->CardTable[i].color, ColorTable[j]);
    							strcpy( newgame->CardTable[i].value , ValueTable[k]);
    							i++;
    						}
    				}
    }
     
    /**
    *\ Permute randomly all the cards structs in cardgame struct
    *\param struct cardgame ShuffledGame : game to shuffle
    *\return struct cardgame ShuffledGame
    */
     
    void Set_Shuffled_CardGame(struct cardgame * ShuffledGame){
    	int i;
    	struct card temp ;
    	srand(time(NULL)) ;
    	for(i=0;i<52;i++){
    		int alea = Generate_Rand(0,51);
    		printf("%d \n",alea) ;
    		temp = ShuffledGame->CardTable[i];
    		ShuffledGame->CardTable[i] = ShuffledGame->CardTable[alea];
    		ShuffledGame->CardTable[alea] = temp;
    		//printf("i:%d %s \n",i,ShuffledGame->CardTable[i].value);
    		}
    }
     
    /**
    *\brief Give a card to one player ie fill player hand substructure with card from cardgame
    *\param struct gamecard gamecard game to deal with
    *\param struct player *player player to deal to
    *\param int nb_card number of card to deal
    */
     
    void Deal_To_Player_Cards(struct cardgame *cardgame ,struct player *player, int nb_card){
     
    	// Init player's hand (rem : separate function todo?..)
     
    	Init_CardLinkedList(player->hand);
     
    	int i;
    	for(i=0;i<nb_card;i++){
     
    		// We test if player has an empty hand,  cuz of chained list different functions
     
    		if (player->hand->first == NULL){
     
    			// If player has an empty hand -> initialisation of CardLinkedList struct with current card to be dealt (gamecard.position)
     
    			CardLinkedList_Set_Top_Card(cardgame->CardTable[cardgame->position],player->hand);
     
    			// one card has been dealt -> update of the game position
     
    			cardgame->position = cardgame->position++;
    			}
    			// Hand of player is not empty -> we browse CardLinkedList and set last card
    			else{
                        CardLinkedList_Set_Last_Card(cardgame->CardTable[cardgame->position],player->hand);
     
                        cardgame->position = cardgame->position++;
    			}
     
    	}
    }
    cards.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
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
     
    #ifndef CARDS_H
    #define CARDS_H
     
     
    char ColorTable[4][10] = {"spade", "clubs", "heart", "diamond"} ;
     
    char ValueTable[13][6] = { "A" , "two", "three", "four", "five", "six", "seven", "eight", "nine", "T", "J", "Q", "K" } ;
     
    struct card
    {
    	char value[10];
    	char color[8];
    } ;
     
    struct cardgame
    {
    	struct card CardTable[52] ;
    	int Id;
    	int position;
    } ;
     
    struct player
    {
    	char name[20];
    	int wage;
    	int bet;
    	struct CardLinkedListEntryPoints* hand;
    };
     
    /**
    *\struct CardChainedList : Linked list of card struct
    */
     
    struct CardLinkedList
    {
    	struct card current;
    	struct CardLinkedList* next;
    	struct CardLinkedList* prev;
    };
     
    struct CardLinkedListEntryPoints
    {
    	struct CardLinkedList *first;
    	struct CardLinkedList *last;
    };
     
     
     
    /**
    *\brief initialize a doublelinked list
    *\param struct CardLinkedListEntryPoints* EntryPoints ; struct containing pointers towards first and last element.
    */
    void Init_CardLinkedList(struct CardLinkedListEntryPoints* EntryPoints);
     
    /**
    *\brief CardLinkedList_Set_Top_Card insert a card at the beginning of a double linked struct containing cards
    *\param struct card CardToAdd The card to insert
    *\param struct CardLinkedListEntryPoints* EntryPoints struct containing entry points for double linked struct containing cards
    */
     
     
    void CardLinkedList_Set_Top_Card(struct card CardToAdd,struct CardLinkedListEntryPoints* EntryPoints);
     
    /**
    *\brief CardLinkedList_Set_Last_Card insert a card at the end of a double linked struct containing cards
    *\param struct card CardToAdd The card to insert
    *\param struct CardLinkedListEntryPoints* EntryPoints struct containing entry points for double linked struct containing cards
    */
     
    void CardLinkedList_Set_Last_Card(struct card CardToAdd,struct CardLinkedListEntryPoints* EntryPoints);
     
     
    /**
    *\brief count number of cards element in d l l EntryPoints
    *\param struct CardLinkedListEntryPoints* EntryPoints
    *\return int count number of elements
    */
     
    int CardLinkedList_Count_Elements(struct CardLinkedListEntryPoints* EntryPoints);
     
     
    /**
    *\brief Free the memory allocated for a CardLinkList struct
    *\param struct CardLinkedListEntryPoints* EntryPoints EntryPoints to the double linked list to be freed.
    */
     
    void CardLinkedList_Clear(struct CardLinkedListEntryPoints* EntryPoints);
     
    /**
    *\brief init a struct cardgame with sorted card(  A, 2 ,3,etc..)  struct
    *\param struct cardgame newgame
    */
     
    void Init_Sorted_CardGame(struct cardgame * newgame);
     
    /**
    *\ Permute randomly all the cards structs in cardgame struct
    *\param struct cardgame ShuffledGame : game to shuffle
    *\return struct cardgame ShuffledGame
    */
     
    void Set_Shuffled_CardGame(struct cardgame * ShuffledGame);
     
    /**
    *\brief Give a card to one player ie fill player hand substructure with card from cardgame
    *\param struct gamecard gamecard game to deal with
    *\param struct player *player player to deal to
    *\param int nb_card number of card to deal
    */
     
    void Deal_To_Player_Cards(struct cardgame *cardgame ,struct player *player, int nb_card);
     
    #endif
    Merci pour le coup de main

  4. #4
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    36
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 36
    Par défaut
    résolu : thanks to gullradriell

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    static char ColorTable[4][10] = {"spade", "clubs", "heart", "diamond"} ;
    static char ValueTable[13][6] = { "A" , "two", "three", "four", "five", "six", "seven", "eight", "nine", "T", "J", "Q", "K" } ;
    Mettre STATIC devant une déclaration de variable dans un header
    -> Dumbass je suis parfois

  5. #5
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par Kryptonaute
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    static char ColorTable[4][10] = {"spade", "clubs", "heart", "diamond"} ;
    static char ValueTable[13][6] = { "A" , "two", "three", "four", "five", "six", "seven", "eight", "nine", "T", "J", "Q", "K" } ;
    Mettre STATIC devant une déclaration de variable dans un header
    -> Dumbass je suis parfois
    Bah, non. Très mauvaise solution bouffeuse de mémoire...

    Ce sont des invariants (lectures seule) ? Alors :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    /* texts.h */
    #ifndef H_TEXTS
    #define H_TEXTS
     
    extern char const *const ColorTable[4];
    extern char const *const ValueTable[13];
     
    #endif
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    /* texts.c */
    #include "texts.h"
    char const *const ColorTable[] = {"spade", "clubs", "heart", "diamond"} ;
    char const* const ValueTable[] = { "A" , "two", "three", "four", "five", "six", "seven", "eight", "nine", "T", "J", "Q", "K" } ;

  6. #6
    Membre émérite
    Avatar de Freed0
    Profil pro
    Étudiant
    Inscrit en
    Mars 2005
    Messages
    635
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2005
    Messages : 635
    Par défaut
    Citation Envoyé par Emmanuel Delahaye
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    ...
    char const *const ColorTable[] = {"spade", "clubs", "heart", "diamond"} ;
    ...
    Tiens j'ai jamais rencontré ce type de variable. Tu peux m'expliquer pourquoi on y met 2 const ?

    Merci

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

Discussions similaires

  1. Problème pour la compilation séparée
    Par mylha dans le forum Débuter
    Réponses: 2
    Dernier message: 29/03/2011, 16h44
  2. Problème de compilation séparée
    Par vincent.mbg dans le forum C
    Réponses: 5
    Dernier message: 14/06/2010, 10h40
  3. Problème simple de compilation séparée
    Par chrisdayton dans le forum Débuter
    Réponses: 7
    Dernier message: 03/12/2009, 07h50
  4. Problème de compilation séparée
    Par orfix dans le forum C
    Réponses: 9
    Dernier message: 25/06/2007, 11h07
  5. Réponses: 1
    Dernier message: 27/05/2002, 01h44

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