#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Basic question re enum declaration in header


    Hi

    Sorry if this is so basic it is boring but I am starting from scratch here.

    I am looking at using an enum and having its declaration in a header file if that is possible.

    If I put this in my main.c file it compiles ok -

    Code:
     // State Machine Actions
     enum ACTION {
        RUN,
        STOP
     };
    
    enum ACTION Action;
     
    
    /******************************************************************************/
    /* Main Program                                                               */
    /******************************************************************************/
    
    void main(void)
    {
        Action = (enum ACTION) RUN;
    
        if (Action == RUN){
            Action = (enum ACTION) STOP;
        }
    
    }
    However if I put the declaration of the enum in a header file and include it in main.c I get an error that helpfully informs me that it is a syntax error, but no further information. So the link to the error in the output takes me to the foot of the enum declaration in the header.

    Code:
     // State Machine Actions
     enum ACTION {
        RUN,
        STOP
     }; <- Error points to here
    As I want this to be a state machine and the interrupt service routines to be in a separate module, I would like these enums to be available in both.

    Can someone tell me the correct way to achieve this?

    Thanks in advance

    Brad
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    It is hard to tell, but you have to realise that there is no magic about header files, they are simply inserted into the translation unit by the pre-processor. To that extent any errors issues within a header file needs to be considered in the context of whatever precedes and follows the header, including all include and macro expansion and conditional compilation etc.

    To avoid a number of problems, your header file should have include guards, and it must end in at least one blank line.

    Code:
    #if !defined ACTION_INCLUDE
    #define ACTION_INCLUDE
    
    // State Machine Actions
    enum ACTION 
    {
        RUN,
        STOP
    } ;
    
    #endif //  ACTION_INCLUDE
    <--blank line here
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by clifford
    It is hard to tell, but you have to realise that there is no magic about header files, they are simply inserted into the translation unit by the pre-processor. To that extent any errors issues within a header file needs to be considered in the context of whatever precedes and follows the header, including all include and macro expansion and conditional compilation etc.

    To avoid a number of problems, your header file should have include guards, and it must end in at least one blank line.

    Code:
    #if !defined ACTION_INCLUDE
    #define ACTION_INCLUDE
    
    // State Machine Actions
    enum ACTION 
    {
        RUN,
        STOP
    } ;
    
    #endif //  ACTION_INCLUDE
    <--blank line here
    Thanks Clifford

    I didn't know about the blank line, I'll tuck that away for future reference. :)

    I have been beavering away all day on this and have learnt a lot. My eventual solution was -

    Code:
    #ifndef Action_def
        #define Action_def
        // State Machine Actions
        enum ACTION {
            RUN,
            STOP
        };
    
        extern int Action;
    #endif
    One problem was that I didn't realise I also had to declare a reference to the int Action in just one source file. Then it became visible in all modules.

    I still have one outstanding puzzle, doubtless due to a hole in my understanding, but I have a workaround.

    Got my state machine up and running now and sampling infra red from my TV remote control. That's got to be enough for one day. :)

    Thanks for the help

    Brad

IMN logo majestic logo threadwatch logo seochat tools logo