How to make extern?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to make extern?

3,438 Views
UtopiaTim
Contributor III
Hi Folks,
 
I have been using the following structure (I think Mac provided
something similar in a different thread). - Thanks Mac!!!
 
 
Anyhow, it works great while I am in the file where it is defined,
but I'm having a hard time figuring out what to make 'extern' in a
different file so I can call them by their bit numbers.
 
i.e.  if (time5_sound = 1)
         ...

Thanks,

Tim
 
 
typedef union {
 byte Byte;
 struct {
   byte NC1        :1;
   byte NC2        :1;
   byte time1Sound :1;
   byte time2Sound :1;
   byte time3Sound :1;
   byte time4Sound :1;
   byte time5Sound :1;
   byte time6Sound :1;
 } Bits;
} time_SoundSTR;  
time_SoundSTR _time_Sound;
#define time_Sound     _time_Sound.Byte
#define NC1            _time_Sound.Bits.NC1
#define NC2            _time_Sound.Bits.NC2
#define time1Sound     _time_Sound.Bits.time1Sound
#define time2Sound     _time_Sound.Bits.time2Sound
#define time3Sound     _time_Sound.Bits.time3Sound
#define time4Sound     _time_Sound.Bits.time4Sound
#define time5Sound     _time_Sound.Bits.time5Sound
#define time6Sound     _time_Sound.Bits.time6Sound
 
 
 
 
Labels (1)
Tags (1)
0 Kudos
15 Replies

1,134 Views
Lundin
Senior Contributor IV
As a sidenote, a better solution is to do like this:

typedef unsigned char time_Sound;

#define timeSound1 0x04
...

timeSound |= timeSound1;


Because the code you have posted does not follow the ANSI/ISO standard, which does not allow char type for bitfields. The code may not compile on another compiler. Also, the code relies heavily on the unspecified behavior of bitfields. Padding, alignment, bit order, big/little endian... all of it is unspecified behavior. So the code will almost certainly behave differently on another compiler/OS/processor.

Therefore I advise to use bit masks as shown above instead. The actual machine code will end up identical.
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Hi Daniel/Lundin,
 
Thanks for the replys.
 
Let me clarify a bit:
 
Daniel -  In the file that includes the calling of the bits, I included what
              you suggested:
 
#include "sound.h"
time_TimeSTR _Time_Sound;
 
With the header file (sound.h) as follows, the compiler is giving me an error
that says I need a ; at the end of time_TimeSTR _Time_Sound; (which is already there).
 
The header file (sound.h) is:

#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE          // force it to be on zero page
#ifndef time_SOUND_HEADER
#define time_SOUND_HEADER
typedef union {
 byte Byte;
 struct {
   byte NC1       :1;
   byte NC2       :1;
   byte time1Sound :1;
   byte time2Sound :1;
   byte time3Sound :1;
   byte time4Sound :1;
   byte time5Sound :1;
   byte time6Sound :1;
 } Bits;
} time_SoundSTR; 
 
extern time_SoundSTR _time_Sound;
 
#define time_Sound     _time_Sound.Byte
#define NC1            _time_Sound.Bits.NC1
#define NC2            _time_Sound.Bits.NC2
#define time1Sound     _time_Sound.Bits.time1Sound
#define time2Sound     _time_Sound.Bits.time2Sound
#define time3Sound     _time_Sound.Bits.time3Sound
#define time4Sound     _time_Sound.Bits.time4Sound
#define time5Sound     _time_Sound.Bits.time5Sound
#define time6Sound     _time_Sound.Bits.time6Sound
#endif
 
 
Lundin:
 
Are you suggesting that I change the sound.h file to the
following?
 
pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE   // force next to be on zero page
 
typedef unsigned char time_Sound;

#define timeSound1 0x04
#define timesound2 0x08
#define timesound3 0x10
#define timesound4 0x20
#define timesound5 0x40
#define timesound6 0x80
 
timeSound |= timeSound1;
Thanks,
 
Tim
0 Kudos

1,134 Views
Lundin
Senior Contributor IV
Yes, that is what I suggest. That code is perfectly safe and portable ISO C.
0 Kudos

1,134 Views
bigmac
Specialist III
Hello Tim,
 
I think the following summarizes what allawtterb and others may have been suggesting -
 
#include "sound.h"
 
#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE
time_SoundSTR _time_Sound;
#pragma DATA_SEG DEFAULT
 
With the header file (sound.h) as follows:

#ifndef time_SOUND_HEADER
#define time_SOUND_HEADER
 
typedef union {
 byte Byte;
 struct {
   byte NC1       :1;
   byte NC2       :1;
   byte time1Sound :1;
   byte time2Sound :1;
   byte time3Sound :1;
   byte time4Sound :1;
   byte time5Sound :1;
   byte time6Sound :1;
 } Bits;
} time_SoundSTR; 
 
#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE
extern time_SoundSTR _time_Sound;
#pragma DATA_SEG DEFAULT
 
#define time_Sound     _time_Sound.Byte
#define NC1            _time_Sound.Bits.NC1
#define NC2            _time_Sound.Bits.NC2
#define time1Sound     _time_Sound.Bits.time1Sound
#define time2Sound     _time_Sound.Bits.time2Sound
#define time3Sound     _time_Sound.Bits.time3Sound
#define time4Sound     _time_Sound.Bits.time4Sound
#define time5Sound     _time_Sound.Bits.time5Sound
#define time6Sound     _time_Sound.Bits.time6Sound
 
#endif
 
Regards,
Mac
 
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Hi Mac,
 
That looks like it took out all the errors.
 
Thanks for once again for helping out!
 

Tim
0 Kudos

1,134 Views
allawtterb
Contributor IV


UtopiaTim wrote:
 
#include "sound.h"
time_TimeSTR _Time_Sound;

If this is a copy from your sound.c file, the problem is time_TimeSTR is not the typedef name you declared in the header, it is time_SoundSTR.  Also, the extern is defined as _time_Sound, time with a lower case t instead of the upper case you have used here.
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Hi allawtterb,
 
Thanks - had some cockpit problems here with the upper/lower case, but got it straightened out.
 
Now we're back to the "Fixup overflow" error messages again.
 
In the code windows, all of the code has 'turned blue', so the compiler recognizes it.
 
The error code:
 
L1907: Fixup overflow in <Object>, type <objType> at offset <Address>[DISABLE, INFORMATION, WARNING, ERROR] DescriptionAn illegal relocation of an object is detected in the object file <Object> at address <Address>. The type of the object is given in <objType>. Tips
  • Check the relocation at that address. The offset may be out of range for this relocation type. If not it may be caused by a corrupt object file.
  • Check if all objects are allocated in the correct area. Is the object correctly declared?
  • This error might occur if the zero paged variables are allocated out of the zero page.

In sound.h, I have specified that it should be on the 1st page, have I missed anything in that regard?

 

Thanks,


Tim

0 Kudos

1,134 Views
allawtterb
Contributor IV
I'm no compiler expert but I think the problem could be that the extern is placed in the zero page and the declaration in sound.c is being placed in default RAM.  When you declare _time_Sound in sound.c try doing:
 
#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE
time_SoundSTR _time_Sound;
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Hi allawtterb,
 
I tried that, & got the same 17 errors.
 
Maybe someone else will pop in & recommend.
 
Thanks!!
 
Tim
0 Kudos

1,134 Views
allawtterb
Contributor IV
How about setting the data page back to the default page after your declarations.  At the end of the header file put
#pragma DATA_SEG DEFAULT_RAM
 
Also put this after your delcaration of _time_Sound in the sound.c file.
0 Kudos

1,134 Views
UtopiaTim
Contributor III
When it's a wierd problem like this, I like to get back to basics.
 
I started a new project in CW, with only main.c, & sound.h (as well as the other
files it automatically generated).
 
I had a single line of code
 
time1Sound = 1;
 
Jumped into the simulator, & the bit set.  Sooooooo, there's something else not quite
right in my <large> program.
 
Thanks again for the help - looks like the structures suggested work fine.
 
Tim
0 Kudos

1,134 Views
allawtterb
Contributor IV
What you need to do is make a header file.  The header file can then be included in any other source file (or even included in another header file) so that you can use these types throughout your program.  You would place the typdef, #defines and "extern time_SoundSTR _time_Sound;" in the header file.  Here is what your header file might look like:
Code:
#ifndef TIME_SOUND_HEADER  // These 2 lines are an include guard, to make sure this file#define TIME_SOUND_HEADER  // is only included once in any one source filetypedef union { byte Byte; struct {   byte NC1        :1;   byte NC2        :1;   byte time1Sound :1;   byte time2Sound :1;   byte time3Sound :1;   byte time4Sound :1;   byte time5Sound :1;   byte time6Sound :1; } Bits;} time_SoundSTR;   extern time_SoundSTR _time_Sound;#define time_Sound     _time_Sound.Byte#define NC1            _time_Sound.Bits.NC1#define NC2            _time_Sound.Bits.NC2#define time1Sound     _time_Sound.Bits.time1Sound#define time2Sound     _time_Sound.Bits.time2Sound#define time3Sound     _time_Sound.Bits.time3Sound#define time4Sound     _time_Sound.Bits.time4Sound#define time5Sound     _time_Sound.Bits.time5Sound#define time6Sound     _time_Sound.Bits.time6Sound#endif  // End of the include guard

 
Also, the if statement you posted should have used a "==" instead of a single equal sign.
 
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Thanks for the info allawtterb - I'll try it out.
 
Also - yep, seems I have a habit of only using
one = !!  I'm slowly breaking the habit!
 
Thanks,
 
Tim
 
 
0 Kudos

1,134 Views
UtopiaTim
Contributor III
Hi allawtterb,
 
I made the data you suggested a file called 'sound.h' .
 
Put the include 'sound.h' at the top of the file that uses
the bit numbers. (as well as adding it to the 'includes' in CW).
 
I am getting a lot of "Fixup overflow in Do_mode2, to _time_sound type 1 at offset..."
 
Perhaps I misunderstood what you were suggesting.
 
Thanks!
 
Tim
0 Kudos

1,134 Views
CompilerGuru
NXP Employee
NXP Employee
Did you define the variables after include the header file
in one C file?
Usually that is the C file with the corresponding name, e.g.

Code:
/* sound.c */#include "sound.h"time_SoundSTR _time_Sound;


Daniel

0 Kudos