In article <66**********************************@v17g2000hsa. googlegroups.com>,
Tricky <Tr********@gmail.comwrote:
>When I try and link together 3 object files, I get the following
problem:
>main.o:main.c:(.rdata+0x0): multiple definition of `_BAD_IMAGE'
>The problem in question comes from the following header file, which is
included in all of the object files:
>#define FILE_BUFFER_SIZE 256
>typedef struct
{
//image dimensions
int pixPerLine;
int numLines;
//image data
char *data;
} image_t;
>const image_t BAD_IMAGE = {-1, -1, 0x0};
>This is the only time BAD_IMAGE is defined.
>Any thoughts on where Im going wrong?
const does not define a constant: it indicates that the values
are read-only (when accessed through that name.)
You are thus attempting to define actual storage for the object
BAD_IMAGE in each of your files that includes the header,
and you are doing so at file scope and you are not using the
'static' keyword so each of the definitions is a global definition.
So you are ending up with a global definition for BAD_IMAGE in
each of the translation units, and of course your linker is complaining.
To resolve this, you need to give a -definition- for BAD_IMAGE in
only one translation unit, and in each other translation unit,
you need to
extern const image_t BAD_IMAGE;
to -reference- the global BAD_IMAGE without defining it in those other
places.
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec