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.
#include .... And making an enumerated type global - Mike Raines - Jul 21 19:51:33 2008
I am having trouble with error msgs about multiple definitions of
variables in various .h files. I am using the following to prevent .h
files from being included more than once:
#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H
...
define vars here...
#endif
It seems that I should be able to define variables in a header file
and then include that header file from any source file that needed to
access the variables. The #ifndef THIS_FILE_NAME_H ... #define
THIS_FILE_NAME_H ... define vars here ... #endif should keep the
variables in the header file from being declared more than once no
matter how many times the header file is included. Why doesn't this work?
I was able to get the code to work, (sort of), by putting the
definitions in the source files and the declarations (as extern)in the
corresponding header files that are included by the source file.
But when I use this method, I cannot figure out how to declare enums
as extern. For instance, in the source file (and other source files)
I want to use the enum members to index into several arrays. What do
I put in the header files to make the enum members accessable to their
associated source files? See the code snippet below:
enum
{
MTL,
CNF,
numberOfErrorMsgs
}errorNumber;
unsigned char errorMsgString[numberOfErrorMsgs][errorMsgSize] = {
"Message Too Long",
"Could Not Find Command"
};
Thanks for your help,
Mike Raines
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: #include .... And making an enumerated type global - Bill Knight - Jul 21 19:58:31 2008
Mike
enums are resolved/evaluated by the compiler not by the preprocessor.
They cannot be used to size arrays as your code indicates you are
attempting.
-Bill Knight
R O SoftWare
Mike Raines wrote:
> I am having trouble with error msgs about multiple definitions of
> variables in various .h files. I am using the following to prevent .h
> files from being included more than once:
> #ifndef HEADER_FILE_NAME_H
> #define HEADER_FILE_NAME_H
> ...
> define vars here...
> #endif
> It seems that I should be able to define variables in a header file
> and then include that header file from any source file that needed to
> access the variables. The #ifndef THIS_FILE_NAME_H ... #define
> THIS_FILE_NAME_H ... define vars here ... #endif should keep the
> variables in the header file from being declared more than once no
> matter how many times the header file is included. Why doesn't this work?
>
> I was able to get the code to work, (sort of), by putting the
> definitions in the source files and the declarations (as extern)in the
> corresponding header files that are included by the source file.
> But when I use this method, I cannot figure out how to declare enums
> as extern. For instance, in the source file (and other source files)
> I want to use the enum members to index into several arrays. What do
> I put in the header files to make the enum members accessable to their
> associated source files? See the code snippet below:
>
> enum
> {
> MTL,
> CNF,
> numberOfErrorMsgs
> }errorNumber;
>
> unsigned char errorMsgString[numberOfErrorMsgs][errorMsgSize] = {
> "Message Too Long",
> "Could Not Find Command"
>
> };
>
> Thanks for your help,
> Mike Raines
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: #include .... And making an enumerated type global - sbur...@netvision.net.il - Jul 22 6:11:44 2008
Putting your definitions in the source file and extern in the header file is the right way
to go.
For your enum trouble, use typedef, like this:
foo.c
-----
#include "foo.h"
char *errorMsgString[numberOfErrorMsgs] = {
"Message Too Long",
"Could Not Find Command"
};
foo.h
-----
typedef enum
{
MTL,
CNF,
numberOfErrorMsgs
}errorNumber;
extern char *errorMsgString[numberOfErrorMsgs];
----------------------
I am having trouble with error msgs about multiple definitions of
>variables in various .h files. I am using the following to prevent .h
>files from being included more than once:
>#ifndef HEADER_FILE_NAME_H
>#define HEADER_FILE_NAME_H
>...
>define vars here...
>#endif
>It seems that I should be able to define variables in a header file
>and then include that header file from any source file that needed to
>access the variables. The #ifndef THIS_FILE_NAME_H ... #define
>THIS_FILE_NAME_H ... define vars here ... #endif should keep the
>variables in the header file from being declared more than once no
>matter how many times the header file is included. Why doesn't this work?
>
>I was able to get the code to work, (sort of), by putting the
>definitions in the source files and the declarations (as extern)in the
>corresponding header files that are included by the source file.
>But when I use this method, I cannot figure out how to declare enums
>as extern. For instance, in the source file (and other source files)
>I want to use the enum members to index into several arrays. What do
>I put in the header files to make the enum members accessable to their
>associated source files? See the code snippet below:
>
>enum
>{
>MTL,
>CNF,
>numberOfErrorMsgs
>}errorNumber;
>
>unsigned char errorMsgString[numberOfErrorMsgs][errorMsgSize] = {
>"Message Too Long",
>"Could Not Find Command"
>
>};
>
>Thanks for your help,
>Mike Raines
>
>------------------------------------
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )
Re: #include .... And making an enumerated type global - Mike Raines - Jul 22 9:07:43 2008
Bill,
Thanks for the response. Actually, enums can be used to size
arrays. That part of the code is working fine. It is my
understanding that enums are basically integers.
I wanted to use the enums to INDEX INTO the arrays of strings.
This works fine in the source file in which they are defined, but my
question was/is:
How do I make the enums visible and usable in other source files in
the project? declaring "extern enum enumName" doesn't work... Any ideas?
Thanks again,
Mike Raines
--- In m...@yahoogroups.com, Bill Knight
wrote:
>
> Mike
> enums are resolved/evaluated by the compiler not by the
preprocessor.
> They cannot be used to size arrays as your code indicates you are
> attempting.
>
> -Bill Knight
> R O SoftWare
>
> Mike Raines wrote:
> > I am having trouble with error msgs about multiple definitions of
> > variables in various .h files. I am using the following to prevent .h
> > files from being included more than once:
> > #ifndef HEADER_FILE_NAME_H
> > #define HEADER_FILE_NAME_H
> > ...
> > define vars here...
> > #endif
> > It seems that I should be able to define variables in a header file
> > and then include that header file from any source file that needed to
> > access the variables. The #ifndef THIS_FILE_NAME_H ... #define
> > THIS_FILE_NAME_H ... define vars here ... #endif should keep the
> > variables in the header file from being declared more than once no
> > matter how many times the header file is included. Why doesn't
this work?
> >
> > I was able to get the code to work, (sort of), by putting the
> > definitions in the source files and the declarations (as extern)in the
> > corresponding header files that are included by the source file.
> > But when I use this method, I cannot figure out how to declare enums
> > as extern. For instance, in the source file (and other source files)
> > I want to use the enum members to index into several arrays. What do
> > I put in the header files to make the enum members accessable to their
> > associated source files? See the code snippet below:
> >
> > enum
> > {
> > MTL,
> > CNF,
> > numberOfErrorMsgs
> > }errorNumber;
> >
> > unsigned char errorMsgString[numberOfErrorMsgs][errorMsgSize] = {
> > "Message Too Long",
> > "Could Not Find Command"
> >
> > };
> >
> > Thanks for your help,
> > Mike Raines
>
------------------------------------

(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )Re: Re: #include .... And making an enumerated type global - Bill Knight - Jul 22 10:38:11 2008
Mike
I was working from long term memory in my previous response so won't
argue the point there. As to the problem you want to solve, try using
typedef
In the .H file
typedef enum
{
MTL,
CNF,
numberOfErrorMsgs
} errorNumber_t;
extern errorNumber_t errorNumber;
In the .C file where it is instantiated
errorNumber_t errorNumber;
Regards
-Bill Knight
R O SoftWare
Mike Raines wrote:
> Bill,
> Thanks for the response. Actually, enums can be used to size
> arrays. That part of the code is working fine. It is my
> understanding that enums are basically integers.
> I wanted to use the enums to INDEX INTO the arrays of strings.
> This works fine in the source file in which they are defined, but my
> question was/is:
> How do I make the enums visible and usable in other source files in
> the project? declaring "extern enum enumName" doesn't work... Any ideas?
>
> Thanks again,
> Mike Raines
>
> --- In m...@yahoogroups.com, Bill Knight
wrote:
>> Mike
>> enums are resolved/evaluated by the compiler not by the
> preprocessor.
>> They cannot be used to size arrays as your code indicates you are
>> attempting.
>>
>> -Bill Knight
>> R O SoftWare
>>
>> Mike Raines wrote:
>>> I am having trouble with error msgs about multiple definitions of
>>> variables in various .h files. I am using the following to prevent .h
>>> files from being included more than once:
>>> #ifndef HEADER_FILE_NAME_H
>>> #define HEADER_FILE_NAME_H
>>> ...
>>> define vars here...
>>> #endif
>>> It seems that I should be able to define variables in a header file
>>> and then include that header file from any source file that needed to
>>> access the variables. The #ifndef THIS_FILE_NAME_H ... #define
>>> THIS_FILE_NAME_H ... define vars here ... #endif should keep the
>>> variables in the header file from being declared more than once no
>>> matter how many times the header file is included. Why doesn't
> this work?
>>> I was able to get the code to work, (sort of), by putting the
>>> definitions in the source files and the declarations (as extern)in the
>>> corresponding header files that are included by the source file.
>>> But when I use this method, I cannot figure out how to declare enums
>>> as extern. For instance, in the source file (and other source files)
>>> I want to use the enum members to index into several arrays. What do
>>> I put in the header files to make the enum members accessable to their
>>> associated source files? See the code snippet below:
>>>
>>> enum
>>> {
>>> MTL,
>>> CNF,
>>> numberOfErrorMsgs
>>> }errorNumber;
>>>
>>> unsigned char errorMsgString[numberOfErrorMsgs][errorMsgSize] = {
>>> "Message Too Long",
>>> "Could Not Find Command"
>>>
>>> };
>>>
>>> Thanks for your help,
>>> Mike Raines
------------------------------------

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