I have several 450 element character arrays (storing bitmap data to display on lcd screens.) I would like to put them under a header file and #define
them, but I keep getting compilation errors. How would I do this in C?
#define numbers[450] {0, 1,etc...}
#define numbers {0, 1, etc...}
#define numbers[450]
then set the numbers later
and many more...
Well... you certainly don't need to use a define. Just add them into the header as const, static arrays.
Also, if you want help on a compilation error then you should post your code.
Becuse you are displaying on an LCD, I am assuming this is an embedded system.
Don't put the data into a header.
Put the data into an ordinary C or C++ file. Compile this. It might only contain the data, that is okay, and makes it easy to update.
Then use the header file to give access to the data.
For example, in a images.c file:
The the images.h is:
Then all other .c files in the program can access them.
It is (almost) always a bad idea to put a definition of a variable into a header file.
A declaration of a variable, eg.
extern byte numbers2[MAX_NUMBERS2];
is telling the C compiler that there is an array variable callednumbers2
somewhere else in the final, linked program. If the linker doesn't get that definition (from somewhere else) then it will raise an error because there is no space for the variable allocated.A definition of a variable (notice no extern), eg.
byte numbers2[MAX_NUMBERS2];
is effectively telling the C compiler that there is an array variable callednumbers2
and it should allocate the space here, in the resulting object code from this source file, and this will be used to hold the value of the variable in the final, linked program.The space for
numbers2
is not allocated by the C compiler when it sees a declaration (preceded byextern
), it is allocated when it sees the actual definition (noextern
).So, if you put the actual definition of any variable in a header file, and include it into more than one source code files (.c), the C compiler will allocate space for the variable more than once. Then the linker will give an error (usually multiple definitions of the same name).
There is a more subtle problem. If, when first developing the program, the header file is only included is one source file, then the program will compile and link correctly. Then, at a later date, if a second source file includes the header (maybe someone has just split the original source code file into two files), the linker will raise a 'multiple definitions' error. This can be very confusing because the program used to compile and link, and apparently nothing has changed.
Summary
Never allocate space for a variable by putting a definition in a header file. Only put variable declarations in header files.
I have had a similar problem. In my case, I needed an array of constants in order to use as size of other static arrays. When I tried to use the
and then declare:
I get an error from my compiler:
So, finally I did the following (Maybe there are more elegant ways to do that):