JustGreg
JustGreg

Reputation: 241

Typedeffed struct is undefined although it is defined

I'm receiving an error message from the compiler indicating (it's an embedded C compiler):

Error[Pe020]: identifier "MPTR" is undefined in myflash.h (see code below)

I'm using a typedeffed struct called MPTR. The code is:

//datahandler.h
#ifndef DATAHANDLER_H
#define DATAHANDLER_H

#include "enet_uip_ap01.h"

typedef struct _MPTR {

unsigned int flash_start;
unsigned int flash_pagestart;

unsigned short rom_start;
unsigned short rom_nextrecord;
unsigned short rom_lastrecord;
} MPTR;

...etc

I want to forward declare a function using an MPTR argument in another header:

//myflash.h
#ifndef MYFLASH_H
#define MYFLASH_H

#include "enet_uip_ap01.h"


unsigned char FLASH_StorePage(MPTR *mptr, unsigned short addr, unsigned int flag);

Both headers: "datahandler.h" and "myflash.h" are included in "enet_uip_ap01.h". However, the above function declaration throws a compilation error.

Most likely it is a plain basic C language issue and it's a shame that I don't know but I'm out of ideas how to solve it. I'd be very glad if someone could point me to my error. Thank you!

EDIT: placed additional code here:

//enet_uip_ap01.h
#ifndef ENET_UIP_AP01_H
#define ENET_UIP_AP01_H

//....other not relevant includes here...
#include "datahandler.h"
#include "myflash.h"

#endif

Upvotes: 3

Views: 13037

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409176

Like Ambroz Bizjak says, it's probably a circular-dependency issue.

In myflash.h, before the function declaration, place the following line:

typedef struct _MPTR MPTR;

This way you declare the type so it can be used in declarations before the structure is defined.

Upvotes: 0

Ambroz Bizjak
Ambroz Bizjak

Reputation: 8095

Seems like a circular include: enet_uip_ap01.h includes datahandler.h, and datahandler.h includes enet_uip_ap01.h. (same goes for myflash.h)

Suppose a source file just includes datahandler.h:

  1. datahandler.h will include enet_uip_ap01.h (before MPTR is defined),
  2. enet_uip_ap01.h will include datahandler.h, which does nothing, because DATAHANDLER_H was defined in step (1),
  3. enet_uip_ap01.h will try to use MPTR, but MPTR is not defined (in will be defined after the enet_uip_ap01.h include - it is not available within this include).

A possible solution is to make datahandler.h (and myflash.h) not include enet_uip_ap01.h; if they need something that enet_uip_ap01.h gives you, they have to include that thing specifically, assuming it is not defined directly in enet_uip_ap01.h.

To sum it up, you need to refactor your includes such that the "includes graph" has no cycles.

Upvotes: 8

Related Questions