Another suggestion is to never allocate anything in a h-file. Allocating variables inside h-files is considered bad programming practice and is forbidden by well-known coding standards such as MISRA-C.
The main reason is to avoid spaghetti code, but there is also a very good practical reason for this: if you allocate a variable in a h-file and then include that h-file from several c-files, you will most likely get linker errors. This is true for Codewarrior and for many other linkers as well.
The quick and (just slightly less) dirty way is to set every variable in the h-file as extern, and then let the actual allocation be done in a corresponding c file.
The good, clean way is to declare the variables as static inside the c-file and then access them through functions, so called private encapsulation.
To answer the question, this is what I would do:
/* h-file */
#ifndef _MY_HEADER_H
#define _MY_HEADER_H
#define CUSTOMER_X
#define CUSTOMER_Y
#define CUSTOMER_Z
...
#define CURRENT_CUSTOMER CUSTOMER_X
/* this is the compiler switch, it is the only thing you will need to change */
void init (void);
int get_some_data (void);
#endif /* _MY_HEADER_H */
/* c-file */
#include "my_header.h"
static int some_data;
static int some_other_data;
...
void init (void)
{
#if CURRENT_CUSTOMER == CUSTOMER_X
some_data = 123;
some_other_data = 456;
#elif CURRENT_CUSTOMER == CUSTOMER_Y
some_data = 111;
some_other_data = 222;
#elif ... /* and so on for each customer */
#else
some_data = default_value;
some_other_data = default_value;
#endif
}
int get_some_data (void)
{
return some_data;
}
/* main.c, identical no matter customer */
#include "my_header.h"
int main()
{
int x;
init();
x = get_some_data();
do_things_with(x);
}
Message Edited by Lundin on
2008-04-01 11:14 AM