简体   繁体   中英

Including typedef enum from header file in another header file

I keep running into an issue while trying to include an enumeration from one header file in antoher. The environment I am working in is embedded C using IAR Embedded Workbench.

I have a header file for dedicated enumerated types named "enums.h"

#ifndef ENUMS_H_
#define ENUMS_H_

typedef enum
{
    SET,
    SCHEDULE,
    EXECUTE
}action_type_t;

#endif

and a header file for a parser named "parser.h"

#ifndef PARSER_H_
#define PARSER_H_

#include "enums.h"
#include <stdint.h>

typedef struct
{
    action_type_t action;
    uint16_t nbytes;
}Message;

#endif

In parser.c I include the header as

#include "parser.h"

When I compile this, I get the error "identifier action_type_t is undefined"

What am I doing wrong here? I am stumped at this point.

Thank you

Your enum definition is missing commas, your parser.h uses uint16_t while having failed to include <stdint.h> and, to be extra pedantic, your include guard macro is encroaching on the reserved namespace because it starts with _ and a capital letter.

This should work:

enums.h :

#ifndef ENUMS_H_
#define ENUMS_H_

typedef enum
{
    SET,
    SCHEDULE,
    EXECUTE, /*the last comma is optional*/
}action_type_t;

#endif

parser.h :

#ifndef PARSER_H_
#define PARSER_H_

#include "enums.h"
#include <stdint.h>

typedef struct
{
    action_type_t action;
    uint16_t nbytes;
}Message;

#endif

Thank you to all who answered, I figured I would come back and close this one. It turns out I had an identically named, but empty header file included in my project...

Next time i'll be better about looking in my own backyard first before asking others.

However PSkocik did provide a working example, and his code compiles perfectly for anyone who stumbles into this thread!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM