0

Ok, so how I understand #include works is by looking up what you are including and complies that and replaces it where the include is. however, when I assume this, my program doesn't compile and it gives __ is undefined all over the place.

for example when in my main.c will have something like

#include "tim.h"
#include "tim_cfg.h"
#include "tim_api.h"

tim.h contains some typesdefs like

typedef enum
{
RATE_DIV1 = 0X0,
RATE_DIV2 = 0X1,
RATE_DIV3 = 0X2,
RATE_DIV4 = 0X3,
RATE_DIV5 = 0X4,
RATE_DIV6 = 0X5,
RATE_DIV7 = 0X6,
RATE_DIV8 = 0X7,
RATE_DIV9 = 0X8
} BaseRate_T;

typedef unsigned char byte; 

tim_cfg.h contains register locations and basic structs

typedef struct
{
byte  TimerSize;         
byte  InterruptLevel;    
} TIMInfo_T;

and tim_api.h contains the function prototypes of the tim functions

So, the problem is why do I get errors

identifier "byte" is undefined

When it the first thing I include?

5
  • shouldnt the typedef take care of the error which is included in the first include Commented Mar 28, 2014 at 19:46
  • should'nt the typedef in the first include link the two? Commented Mar 28, 2014 at 19:48
  • Does tim.h also itself include tim_api.h at a point before where the typedef is? If it's not too long, post the entire tim.h, or at least the portion up to and including TIMInfo_T. Commented Mar 28, 2014 at 20:04
  • If you reduce it to just the typedef, the struct, and a main program (to print sizeof(TIMInfo_T), say), it works fine, so something else is going on. Does tim_api.h have code in it? That sometimes has weird effects. Commented Mar 28, 2014 at 23:06
  • tim_api.h just has extern voids in it Commented Mar 31, 2014 at 13:43

1 Answer 1

1

You should follow the rule that every header file should include what it needs to work.

With the setup you have, if someone includes tim_cfg.h on its own, the byte type is not defined.

A better solution would be:

tim_cfg.h:
    #include "tim.h"
    typedef struct
    {
        byte  TimerSize;         
        byte  InterruptLevel;    
    } TIMInfo_T;

That way, everything that is needed for tim.cfg.h is there when you include it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.