3

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
4
  • 2
    Could you post your enet_uip_ap01.h? Does struct _MPTR* in the second header work or not?
    – Yuji
    Nov 14, 2011 at 11:33
  • Currently pasted code should not any issue, pls post enet_uip_ap01.h
    – Avinash
    Nov 14, 2011 at 11:37
  • try including datahandler.h here
    – Kris
    Nov 14, 2011 at 11:37
  • Sorry, how can I place code here?
    – JustGreg
    Nov 14, 2011 at 11:38

2 Answers 2

8

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.

2
  • 1
    Detecting circular references (although sometimes unavoidable) should trigger one's rethink_my_design() or at least the rethink_my_code_organisation() method ... ;-)
    – alk
    Nov 14, 2011 at 11:54
  • Ok, I've just cleaned my code, splitted some headers, made separate includes and now it is running fine. Thank you!
    – JustGreg
    Nov 14, 2011 at 13:04
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.