une boullette dans mon .h [C] - Programmation
Marsh Posté le 15-02-2001 à 13:49:38
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
Marsh Posté le 15-02-2001 à 14:04:04
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
Marsh Posté le 15-02-2001 à 14:25:38
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
Marsh Posté le 15-02-2001 à 14:47:02
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
enfin bon, le principal c'est que ça marche mais ça fait chier de bloquer pour des conneries
merci de votre aide à tous
Marsh Posté le 15-02-2001 à 22:25:13
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 !
Marsh Posté le 16-02-2001 à 11:09:54
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....
Marsh Posté le 15-02-2001 à 12:12:59
#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
pourquoi il me dit ça juste pour ce tableau ?