i often end up in a situation where i've prototyped something in a small testspace then things work out and you want to reuse those tools as you build out another project in the same solution. i've run into this twice in the past year and spent 10 minutes resolving a misunderstanding of intent between the compiler and myself so i'm posting here to save time in the future. note this is for an internal test utility and not product code which shouldn't have any (or very few) globals in the first place, so i'm not putting this here as a shining example of how to do something but instead as how i resolve this specific issue.
here's the refactoring steps:
1. put the typedef and structure in the header file.
typedef struct foo_s { } foo_t;
2. put the declaration into one of the files (the central utility file for example):
foo_t foo;
3. when you want to reference the variable foo in another .c file, use this:
extern foo_t foo;
Enums follow the same pattern, put this in a header:
typedef enum eMODEL_MODE {
SINGLE_MODEL,
MULTI_MODEL,
MANY_MODEL
} eModelMode_t;
Declare the value into one of the other files:
eModelMode_t g_eModelMode = SINGLE_MODEL;
Reference like so:
extern eModelMode_t g_eModelMode;
here's the refactoring steps:
1. put the typedef and structure in the header file.
typedef struct foo_s { } foo_t;
2. put the declaration into one of the files (the central utility file for example):
foo_t foo;
3. when you want to reference the variable foo in another .c file, use this:
extern foo_t foo;
Enums follow the same pattern, put this in a header:
typedef enum eMODEL_MODE {
SINGLE_MODEL,
MULTI_MODEL,
MANY_MODEL
} eModelMode_t;
Declare the value into one of the other files:
eModelMode_t g_eModelMode = SINGLE_MODEL;
Reference like so:
extern eModelMode_t g_eModelMode;
No comments:
Post a Comment