The purpose of this group is to foster exchange of information on the Texas Instruments MSP430 family of microcontrollers and related tools. Everyone welcome, all levels of familiarity/expertise.
global variable for multiple file - magzky02 - Jul 31 21:37:21 2008
my code is very long. I used to place all my codes in a single file.
Now i want to split them so that 1 module will be placed on one file.
My global variable declared in main.c is having error "undefined" in
module1.c. I tried to redeclare in module1.c, but it has error like
redefined. How to declare a global variable that can be used in
multiple file? im using IAR.
regards,
-mago-
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: global variable for multiple file - mago Umandam - Jul 31 22:44:21 2008
=A0Ohh... I redefined in the module using "extern' no more error :)
--- On Fri, 8/1/08, magzky02
wrote:
From: magzky02
Subject: [msp430] global variable for multiple file
To: m...@yahoogroups.com
Date: Friday, August 1, 2008, 9:37 AM
my code is very long. I used to place all my codes in a single file.=20
Now i want to split them so that 1 module will be placed on one file.=20
My global variable declared in main.c is having error "undefined" in=20
module1.c. I tried to redeclare in module1.c, but it has error like=20
redefined. How to declare a global variable that can be used in=20
multiple file? im using IAR.
regards,
-mago-
=20
=20=20=20=20=20=20
[Non-text portions of this message have been removed]
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: global variable for multiple file - bb_stefan - Aug 1 1:55:21 2008
But that way you must redefine your global var's in every file you
are using them.
Better way is to use seperate global.h and global.c files and do a
#include "global.h" in the files you want to use the var's:
---------------------------------------------
:
#ifndef _global_vars
#define _global_vars
extern unsigned int dummy_int;
extern unsigned char dummy_char;
...
#endif /* #ifndef __global_vars */
----------------------------------------------
---------------------------------------------
:
#include "global.h"
unsigned int dummy_int;
unsigned char dummy_char;
...
---------------------------------------------
> Ohh... I redefined in the module using "extern' no more error :)
> my code is very long. I used to place all my codes in a single file.
> Now i want to split them so that 1 module will be placed on one file.
> My global variable declared in main.c is having error "undefined" in
> module1.c. I tried to redeclare in module1.c, but it has error like
> redefined. How to declare a global variable that can be used in
> multiple file? im using IAR.
>
> regards,
> -mago-
>
>
>
>
> [Non-text portions of this message have been removed]
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: global variable for multiple file - Stuart_Rubin - Aug 1 14:34:43 2008
Actually, in general, you "usually" don't want to use global
variables. They have there place, but only after careful consideration.
Here goes a rant...
Breaking a file into multiple files, we'll call them modules, is
usually a good idea, but the the division should be made on the basis
of the responsibilities of the modules. You should try to abstract
different aspects of your program away from each other. In embedded
programming, this usually means things like separating direct I/O
manipulation, UARTS, timers, etc., into their own modules. Then, you
create an interface ("API") between the modules. This isolates the
modules thoroughly, makes more maintable code, easier to debug, easier
to change hardware, etc. In your problem, you would get rid of the
global variables and share data only though a well-defined interface.
Yes, it's true, you DO pay a small performance price for data accesses
between modules, but it is almost always made up for by a better
overall product.
Stuart
--- In m...@yahoogroups.com, "bb_stefan"
wrote:
>
> But that way you must redefine your global var's in every file you
> are using them.
>
> Better way is to use seperate global.h and global.c files and do a
> #include "global.h" in the files you want to use the var's:
>
> ---------------------------------------------
> :
>
> #ifndef _global_vars
> #define _global_vars
>
> extern unsigned int dummy_int;
> extern unsigned char dummy_char;
> ...
>
> #endif /* #ifndef __global_vars */
> ----------------------------------------------
> ---------------------------------------------
> :
>
> #include "global.h"
> unsigned int dummy_int;
> unsigned char dummy_char;
> ...
> ---------------------------------------------
>
> > Ohh... I redefined in the module using "extern' no more error :)
>
> > my code is very long. I used to place all my codes in a single file.
> > Now i want to split them so that 1 module will be placed on one file.
> > My global variable declared in main.c is having error "undefined" in
> > module1.c. I tried to redeclare in module1.c, but it has error like
> > redefined. How to declare a global variable that can be used in
> > multiple file? im using IAR.
> >
> > regards,
> > -mago-
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > [Non-text portions of this message have been removed]
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: global variable for multiple file - bumerang boom - Aug 2 13:19:33 2008
Or you can do this:
#define EXTERNAL
ifdef EXTERNAL
extern
endif
int some_global_variable;
Without Wax
BB
--- On Fri, 8/1/08, bb_stefan
wrote:
> From: bb_stefan
> Subject: [msp430] Re: global variable for multiple file
> To: m...@yahoogroups.com
> Date: Friday, August 1, 2008, 1:55 AM
> But that way you must redefine your global var's in
> every file you
> are using them.
>
> Better way is to use seperate global.h and global.c files
> and do a
> #include "global.h" in the files you want to use
> the var's:
>
> ---------------------------------------------
> :
>
> #ifndef _global_vars
> #define _global_vars
>
> extern unsigned int dummy_int;
> extern unsigned char dummy_char;
> ...
>
> #endif /* #ifndef __global_vars */
> ----------------------------------------------
> ---------------------------------------------
> :
>
> #include "global.h"
> unsigned int dummy_int;
> unsigned char dummy_char;
> ...
> ---------------------------------------------
>
> > Ohh... I redefined in the module using
> "extern' no more error :)
>
> > my code is very long. I used to place all my codes in
> a single file.
> > Now i want to split them so that 1 module will be
> placed on one file.
> > My global variable declared in main.c is having error
> "undefined" in
> > module1.c. I tried to redeclare in module1.c, but it
> has error like
> > redefined. How to declare a global variable that can
> be used in
> > multiple file? im using IAR.
> >
> > regards,
> > -mago-
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > [Non-text portions of this message have been removed]
> > ------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: global variable for multiple file - Hardy Griech - Aug 2 18:00:53 2008
bumerang boom wrote:
> Or you can do this:
> #define EXTERNAL
>
> ifdef EXTERNAL
> extern
> endif
> int some_global_variable;
You can't! Because what's about
ifdef EXTERNAL
extern
endif
int some_var = 4711;
The below preprocessed statement is invalid:
extern int some_var = 4711;
You can do it that way in a header:
#ifdef GLOBALS_DEFINE
#define DEFVAR( t,v,i ) t v = i
#else
#define DEFVAR( t,v,i ) extern t v
#endif
/** total uptime in seconds */
DEFVAR( uint32_t, totalUptime, 0 );
And in one place of your sources before the include make a
#define GLOBALS_DEFINE
That's it.
Hardy
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: global variable for multiple file - bumerang boom - Aug 2 19:52:07 2008
Perhaps your compiler can't. Mine Can :)
Without Wax,
BB
--- On Sat, 8/2/08, Hardy Griech
wrote:
> From: Hardy Griech
> Subject: Re: [msp430] Re: global variable for multiple file
> To: m...@yahoogroups.com
> Date: Saturday, August 2, 2008, 6:00 PM
> bumerang boom wrote:
> > Or you can do this:
> >
> >
> > #define EXTERNAL
> >
> > ifdef EXTERNAL
> > extern
> > endif
> > int some_global_variable;
>
> You can't! Because what's about
>
> ifdef EXTERNAL
> extern
> endif
> int some_var = 4711;
>
> The below preprocessed statement is invalid:
>
> extern int some_var = 4711;
> You can do it that way in a header:
>
>
> #ifdef GLOBALS_DEFINE
> #define DEFVAR( t,v,i ) t v = i
> #else
> #define DEFVAR( t,v,i ) extern t v
> #endif
>
> /** total uptime in seconds */
> DEFVAR( uint32_t, totalUptime, 0 );
> And in one place of your sources before the include make a
>
> #define GLOBALS_DEFINE
> That's it.
>
> Hardy
>
> ------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: global variable for multiple file - mago Umandam - Aug 4 1:23:35 2008
Hi stuart,
=A0
Your comment is very helpful. And yeah if i got time, i will try to write m=
y code in such a way that i avoid global variables.=20
=A0
regards,
mago
--- On Sat, 8/2/08, Stuart_Rubin
wrote:
From: Stuart_Rubin
Subject: [msp430] Re: global variable for multiple file
To: m...@yahoogroups.com
Date: Saturday, August 2, 2008, 2:34 AM
Actually, in general, you "usually" don't want to use global
variables. They have there place, but only after careful consideration.
Here goes a rant...
Breaking a file into multiple files, we'll call them modules, is
usually a good idea, but the the division should be made on the basis
of the responsibilities of the modules. You should try to abstract
different aspects of your program away from each other. In embedded
programming, this usually means things like separating direct I/O
manipulation, UARTS, timers, etc., into their own modules. Then, you
create an interface ("API") between the modules. This isolates the
modules thoroughly, makes more maintable code, easier to debug, easier
to change hardware, etc. In your problem, you would get rid of the
global variables and share data only though a well-defined interface.
Yes, it's true, you DO pay a small performance price for data accesses
between modules, but it is almost always made up for by a better
overall product.
Stuart
--- In msp430@yahoogroups. com, "bb_stefan" wrote:
>
> But that way you must redefine your global var's in every file you
> are using them.
>=20
> Better way is to use seperate global.h and global.c files and do a
> #include "global.h" in the files you want to use the var's:
>=20
> ------------ --------- --------- --------- ------
> :
>=20
> #ifndef _global_vars
> #define _global_vars
>=20
> extern unsigned int dummy_int;
> extern unsigned char dummy_char;
> ...
>=20
> #endif /* #ifndef __global_vars */
> ------------ --------- --------- --------- -------
>=20
>=20
> ------------ --------- --------- --------- ------
> :
>=20
> #include "global.h"
> unsigned int dummy_int;
> unsigned char dummy_char;
> ...
> ------------ --------- --------- --------- ------
>=20
>=20
>=20
> > Ohh... I redefined in the module using "extern' no more error :)
>=20
>=20
>=20
> > my code is very long. I used to place all my codes in a single file.=20
> > Now i want to split them so that 1 module will be placed on one file.=20
> > My global variable declared in main.c is having error "undefined" in=20
> > module1.c. I tried to redeclare in module1.c, but it has error like=20
> > redefined. How to declare a global variable that can be used in=20
> > multiple file? im using IAR.
> >=20
> > regards,
> > -mago-
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> >=20
> > [Non-text portions of this message have been removed]
>
=20
=20=20=20=20=20=20
[Non-text portions of this message have been removed]
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: global variable for multiple file - Hardy Griech - Aug 4 16:34:39 2008
bumerang boom wrote:
> Perhaps your compiler can't. Mine Can :)
:
Ooops!? Which compiler and how does it behave, if you make a
extern int aa = 10;
in one file, and
extern int aa = 20;
in the other, and finally
int aa = 30;
??
Just wondering...
Hardy
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )