Include order: why must hal.h be included before hal_accelerometer.h, etc.?

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
dvchench
Posts: 1
Joined: Fri Oct 12, 2018 9:11 pm
Has thanked: 1 time

Include order: why must hal.h be included before hal_accelerometer.h, etc.?

Post by dvchench »

I notice that hal_sensors.h does not include anything that provides _base_object_methods . Therefore, hal.h must be included before anything that inherits from hal_sensors.h . What is the reasoning behind this, or is this an oversight?

I normally organize my includes starting with "most local" first and "most global" see the accepted answer here for reasoning: https://stackoverflow.com/questions/276 ... -practices . Does Chibi have different guidance on this issue?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Include order: why must hal.h be included before hal_accelerometer.h, etc.?

Post by Giovanni »

Hi,

There are tens of headers in HAL, all the common header are included in hal.h, the remaining ones depends on things imported by hal.h so are included afterward.

The pattern is, first hal.h then other HAL-related headers.

For simplicity we never hand-pick headers already included by hal.h, we just import the whole thing, the advantage is that if hal.h changes its internal organization then there are no dependencies to fix in other places.

halobject.h is one of those fundamental headers included by hal.h so it is not included separately.

Giovanni
Post Reply