Hello. I'm having hard time to compile things around, especially my HAL.
In this HAL, there is 3 headers. These files are named IOTree.h, IOBus.h, IODevice.h.
IOTree.h #include IOBus.h which itself includes IODevice.h which in turn includes IOTree.h. So, how could so that GCC does not complain and compiles ? Also, is there a better way of including headers in that kind of situation ?
Thanks in advance for any information.
Recursive headers
I ran into the same problem, so either make them into a single file or have a new IO.h file
--Michael
Code: Select all
#ifndef _IO_
#include IOtree.h
#include IObus.h
#include IOdevice.h
#endif
You can use include guards.
IOtree.h:
IObus.h:
and so on...
IOtree.h:
Code: Select all
#ifndef IOTREE_H
#define IOTREE_H
/* the rest of the stuff in IOtree.h */
#endif
Code: Select all
#ifndef IOBUS_H
#define IOBUS_H
/* the rest of the stuff in IObus.h */
#endif
Don't include a header inside a header unless you have to.
Use inclusion guards in all headers.
Include a header in all files that require them, don't rely in implicit indirect inclusion.
You need to include a header when:
- You create an object of that type.
- You instantiate a template with an object of that type.
- You call sizeof on the type
- You call a function on the object
- You use a member variable of the object
You do NOT need the header when
- You use only a pointer to that type.
- You call sizeof on a pointer of the type
- You use an interface it also uses
- You forward a pointer
- You do something with a pointer, that does not use the object.
Use inclusion guards in all headers.
Include a header in all files that require them, don't rely in implicit indirect inclusion.
You need to include a header when:
- You create an object of that type.
- You instantiate a template with an object of that type.
- You call sizeof on the type
- You call a function on the object
- You use a member variable of the object
You do NOT need the header when
- You use only a pointer to that type.
- You call sizeof on a pointer of the type
- You use an interface it also uses
- You forward a pointer
- You do something with a pointer, that does not use the object.