Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extern union

Status
Not open for further replies.

easyts

Programmer
Jan 31, 2007
8
ES
Hi everyone,

I'm finishing a code for a NEC uProcessor and I'm having problems with unions. At the beginning of writing this code I wrote everything in 1 file, but after I decided to divide this code in various modules.

I have some unions that are used in different modules, so I have to declare them as 'extern', right?

The question is that I don't know how to do that...

What I've done is:

In "global.h"

extern __saddr union bts
{ unsigned char byte;
struct
{ unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
} bits1, bits2, bits3, bits4, digi0, digi1;

****************

In other modules, I include "global.h" and the error I get when I use 'bits1', 'bits2' or another one is:

"Undefined external 'bits1' referred in module.c" which is a logical error, but I don't know how to solve this...
 
I forgot to say that if I declare the union as follow in "global.h" (without extern):

__saddr union bts
{ unsigned char byte;
struct
{ unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
} bits1, bits2, bits3, bits4, digi0, digi1;

then the error is:

"Entry 'bits1' in module1 redefined in module2" which is also a ogical error because in module1 and 2 I include "global.h" and I'm redefining the union...
 
The extern keyword declares variables as global, not types. Declare union bts type then declare bit1 et al as extern(als) in a header file:
Code:
union bts
{
...
}; /* or use typedef to declare this union type */
extern union bts bit1, bit2,....;
Now in one and only one source file define these variables:
Code:
#include "header file with declarations"
...
union bts bit1, bit2,....;
So you have unique definitions and may use these variables in all files of your project.

Remember: every new struct/union declares new type. So
Code:
struct X { int x; } a; /* No global declaration of type X */
struct X { int x; } b; /* Error: struct X already defined!*/
struct X c; /* Error: we have only var (a) of this type   */

Apropos: bit fields are not obliged to overlap with char element of your union in predictable manner. Better don't use bit fields at all: use shift (<<,>>) and mask (&) ops. It's another story why...

Oops... Transmit error...
 
Oh! Thanks! You're right!

I propose another shorter solution:

In 'global.h':

extern __saddr union bts
{ unsigned char byte;
struct
{ unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
} bits1, bits2, bits3, bits4, digi0, digi1;


And in the other modules:

__saddr union bts bits1, bits2, bits3, bits4, digi0, digi1;

By the way, how do you make this 'code view'? I mean this window where you write the code.
 
See Process TGML link on the form. TGML is a simple mark-up language of this forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top