I'd like to make a definition of union in one file and call it from others. Union needs to consists structures in it. Is it possible? Thank you, Goran
Yes. Just put the declaration in a header file. #include the header file in the files that want to use the type. MyUnion.h:
typedef struct { U8 blah; U8 bleah; } StructOne; typedef struct { U16 foo; U16 bar; } StructTwo; typedef union { StructOne asStructOne; StructTwo asStructTwo; } MyUnion;
#include "MyUnion" MyUnion mine; ... mine.asStructOne.blah = 0;
Hi Davis, Thank you very much, it works. I needed to call from more than one file. So in the first I did as you show me in MyUnionClient.c. In the second and all others I did like: #include "MyUnion" extern MyUnion mine; and it works,...
If you want to export the global variable as well as just the type, you might want to put the extern MyUnion mine; in the MyUnion.h header file as well. There should be only one actual definition of the variable "mine" in a .c somewhere, but the extern reference can be used by all.
"There should be only one actual definition of the variable "mine" in a .c somewhere, but the extern reference can be used by all." (my emphasis). Note that you can even include the header with the extern reference in the .c source file with the actual definition of the variable; in fact, I recommend that you should do this - this is the only way that you can get the compiler to warn you when your extern reference gets out-of-step with the actual definition of the variable! Note that this is all standard 'C' stuff - nothing specifically to do with Keil at all.