Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3285 connectés 

  FORUM HardWare.fr
  Programmation

  [C] une boullette dans mon .h

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C] une boullette dans mon .h

n°14479
Titoine42
Posté le 15-02-2001 à 12:12:59  profilanswer
 

#ifndef __PUBLIC__
#define __PUBLIC__
t_global  *gbl;
t_block   *block_tab[19];
t_key_func  tab_key_func[]=
{
  {'a', k_down},
  {'b', k_fall},
  {'c', k_left},
  {'d', k_right},
  {'e', k_rotate_left},
  {'f', k_rotate_right},
  {'g', k_pause},
  {'h', k_quit},
  {0, 0}
};
#else
extern t_global  *gbl;
extern t_block  *block_tab[];
extern t_key_func tab_key_func[];
#endif
 
il me chie une erreur : "multiple definition of 'tab_key_func'" au linkage
 
:cry:
 
pourquoi il me dit ça juste pour ce tableau ?

mood
Publicité
Posté le 15-02-2001 à 12:12:59  profilanswer
 

n°14488
Toxin
Carpe ★★ Vitam
Posté le 15-02-2001 à 13:32:11  profilanswer
 

Combien de fois ce .h est inclus au total ???


---------------
"If you can walk away from a landing, it's a good landing. If you use the airplane the next day, it's an outstanding landing." - Chuck Yeager. | Chaîne YT | Photos
n°14489
Titoine42
Posté le 15-02-2001 à 13:48:03  profilanswer
 

ds tout mes fichiers.c

n°14490
Titoine42
Posté le 15-02-2001 à 13:49:38  profilanswer
 

le voici dans son intégralité :
 
#ifndef __XTETRIS_H__
#define __XTETRIS_H__
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
 
#include "my/my.h"
 
#define  TIMER_INTERVAL  150000
#define  DEFAULT_WIDTH  9
#define  DEFAULT_HEIGHT  20
#define  DEFAULT_BLOCK_WIDTH 20
#define  DEFAULT_BLOCK_HEIGHT 20
#define  DEFAULT_BLOCK_SIZE 4
 
typedef struct s_global
{
  int  width;
  int  height;
  int  block_width;
  int  block_height;
  char  **tab;
  char  **tab_tmp;
  int  block;
  int  block_x;
  int  block_y;
}  t_global;
 
typedef struct s_block
{
  int  block_type;
  int  rotation_ref;
  int  max_rotation;
  char  **tab;
}  t_block;
 
typedef struct s_key_func
{
  char  key;
  void  (*f)();
}  t_key_func;
 
int  random(void);
 
int  main(int argc, char **argv);
int  get_arguments(int argc, char **argv);
void  set_default_arguments();
void  init();
void  init_tabs();
void  init_block_list();
void  timer();
void  new_block();
int  test_block_move(int block, int x, int y);
void  write_block_to_tab(int block, int x, int y);
void  update_tab();
void  reset_tab_tmp();
 
void  block_down();
 
void  k_down();
void  k_fall();
void  k_left();
void  k_right();
void  k_rotate_left();
void  k_rotate_right();
void  k_pause();
void  k_quit();
 
void  aff_gbl();
void  aff_tab(char **tab);
void  aff_block_list();
 
void            my_outc(int c);
 
#ifndef __PUBLIC__
#define __PUBLIC__
t_key_func  tab_key_func[]=
{
  {'a', k_down},
  {'b', k_fall},
  {'c', k_left},
  {'d', k_right},
  {'e', k_rotate_left},
  {'f', k_rotate_right},
  {'g', k_pause},
  {'h', k_quit},
  {0, 0}
};
t_global  *gbl;
t_block   *block_tab[19];
#else
extern t_key_func tab_key_func[];
extern t_global  *gbl;
extern t_block  *block_tab[];
#endif
 
#endif

n°14493
Toxin
Carpe ★★ Vitam
Posté le 15-02-2001 à 14:00:25  profilanswer
 

Pour moi tu mets toutes des déclarations en extern et tu vires le #else.


---------------
"If you can walk away from a landing, it's a good landing. If you use the airplane the next day, it's an outstanding landing." - Chuck Yeager. | Chaîne YT | Photos
n°14495
gilou2
Posté le 15-02-2001 à 14:04:04  profilanswer
 

Bah c'est normal.
Ne met pas les variables globales dans ton .h
Avec ton truc, les variables sont instanciées une fois par fichier .c qui inclut le .h
Met les dans un .c


---------------
Développeur de FreeVCR : http://freevcr.ifrance.com [:gilou2]
n°14498
Toxin
Carpe ★★ Vitam
Posté le 15-02-2001 à 14:14:30  profilanswer
 

Vi dans un .c j'avais oublié de le dire. Merci gilou2.


---------------
"If you can walk away from a landing, it's a good landing. If you use the airplane the next day, it's an outstanding landing." - Chuck Yeager. | Chaîne YT | Photos
n°14499
Titoine42
Posté le 15-02-2001 à 14:25:38  profilanswer
 

si c'est pas bon, pourquoi il me fais une erreur que pour le tab_key_func
 
j'ai plein d'autre prog avec des tableaux de pointeurs sur fonction en global qui compile bien

n°14504
Titoine42
Posté le 15-02-2001 à 14:47:02  profilanswer
 

j'ai trouvé comment le faire marcher mais franchement je vois pas la différence avec ce que je faisais.
 
je faisais dans mon .h :
#ifndef __PUBLIC__
#define __PUBLIC__
#else
#endif
et dans mon main.c :
#include "mon.h"
 
maintenant je fais dans mon .h :
#ifdef __PUBLIC__
#else
#endif
et dans mon main.c :
#define __PUBLIC__
#include "mon.h"
#undef __PUBLIC__
 
et là ça marche :sweat:
 
enfin bon, le principal c'est que ça marche mais ça fait chier de bloquer pour des conneries
 
merci de votre aide à tous :jap:

n°14585
z51
Posté le 15-02-2001 à 22:25:13  profilanswer
 

Ton erreur au link vient du fait que tab_key_func est initialisé (à la différence des autres variables).
Pour éviter ça fais ton init dans un des .c (dans une fonction bien sûr).
 
Sinon petite remarque quant au define dans le fichier h :
 
#ifndef _TOTO_H_
#define _TOTO_H_
...
#endif
 
Mieux vaut le remplacer par :
 
#define _TOTO_H_
 
dans le fichier h, et ajouter:
 
#ifndef _TOTO_H_
#include "toto.h"
#endif
 
dans tous les .c qui incluent ce header.
 
Ca accélère pas mal les temps de compilation, parce qu'alors le .h n'est pas ouvert à chaque fois.
(je t'accorde que si t'as 2 fichiers c la différence n'est pas notable :)
 
oili oilou !

mood
Publicité
Posté le 15-02-2001 à 22:25:13  profilanswer
 

n°14621
Toxin
Carpe ★★ Vitam
Posté le 16-02-2001 à 11:09:54  profilanswer
 

Personnellement je préfère la première solution pour des raisons esthétiques et surtout parce que c'est une autoprotection du .h
 
 
Mais bon chacun fait ce qu'il veut avec son c....


---------------
"If you can walk away from a landing, it's a good landing. If you use the airplane the next day, it's an outstanding landing." - Chuck Yeager. | Chaîne YT | Photos

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation

  [C] une boullette dans mon .h

 

Sujets relatifs
Plus de sujets relatifs à : [C] une boullette dans mon .h


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)