Recursive headers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
synthetix
Posts: 15
Joined: Sun Jan 28, 2007 8:45 am
Location: Somewhere in the infinite dimension universe that my vast imagination is.

Recursive headers

Post by synthetix »

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.
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

I ran into the same problem, so either make them into a single file or have a new IO.h file

Code: Select all

#ifndef _IO_
#include IOtree.h
#include IObus.h
#include IOdevice.h
#endif
--Michael
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

You can use include guards.

IOtree.h:

Code: Select all

#ifndef IOTREE_H
#define IOTREE_H

/* the rest of the stuff in IOtree.h */

#endif
IObus.h:

Code: Select all

#ifndef IOBUS_H
#define IOBUS_H

/* the rest of the stuff in IObus.h */

#endif
and so on...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

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.
Post Reply