This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

enum external declaration and use

Hi Folks,
I am trying to use enum to define a set of named constants with values so that they can be used by multiple C files in my project. For example. I use the following snipet to define Months in a headers file externals.h, and then If I then try to use it as follows in any of those files, it does not work.

 enum MONTHS {M1 = 'Jan', M2 = 'Feb', M3 = 'Mar'};
....
....
extern enum MONTHS; // should it be extern enum MONTHS months; ?
....
.....
SendByte (M1);//(This routine display M1 on my serial port).

I get error ---> (...error 230 MONTH unknown struct/union/enum tag)

What is the problem and how can I fix it ?
I would like to get a better understanding of this enum business, if possible..

Thanks.

Bhal Tulpule

Parents
  • Folks,
    I appreciate your rapid response and help.
    I forgot that enum is needs to be a integer not a string. Thanks for pointing it out.
    I think I fixed it and so here is what the files look like:
    externals.h has the extern stmt.

    extern month;
    


    A "main" FILE has the declaration of the enum and some code that uses it...

    enum MONTH {M1 = 0x41, M2 = 0x42, M3 = 0x43} month;
    ...
    


    The program code in that routine (main.c) has some test code to make sure I can manipulate it. This compiles and links ok.

    SendByte (M1);
    month = M1;
    SendByte (month);
    month = 0x49;
    SendByte (month);
    


    However when I try to use the same code above(SendByte...month =... SendByte...) from the main.c file,in another FILE, I get an errors---> M1: undefined identifier.. (that file has include for externals.h)
    So my whole purpose of defining the set of constants M1, M2, M3 in one place and using them everywhere is not still successful.
    Maybe I should use some other construct ?

    Any help would be appreciated.
    Thanks.
    Bhal Tulpule

Reply
  • Folks,
    I appreciate your rapid response and help.
    I forgot that enum is needs to be a integer not a string. Thanks for pointing it out.
    I think I fixed it and so here is what the files look like:
    externals.h has the extern stmt.

    extern month;
    


    A "main" FILE has the declaration of the enum and some code that uses it...

    enum MONTH {M1 = 0x41, M2 = 0x42, M3 = 0x43} month;
    ...
    


    The program code in that routine (main.c) has some test code to make sure I can manipulate it. This compiles and links ok.

    SendByte (M1);
    month = M1;
    SendByte (month);
    month = 0x49;
    SendByte (month);
    


    However when I try to use the same code above(SendByte...month =... SendByte...) from the main.c file,in another FILE, I get an errors---> M1: undefined identifier.. (that file has include for externals.h)
    So my whole purpose of defining the set of constants M1, M2, M3 in one place and using them everywhere is not still successful.
    Maybe I should use some other construct ?

    Any help would be appreciated.
    Thanks.
    Bhal Tulpule

Children
No data