Detect compilation for ChibiOS

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

Posts: 138
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 16 times
Been thanked: 3 times

Detect compilation for ChibiOS

Postby avrhack » Thu Oct 15, 2020 10:42 am

Sure this is a simple one but struggling to figure it out from the various makefiles.....

I have a library routine (.h and .c) that needs to use e.g. chsnprintf instead of sprintf when compiled for ChibiOS. I can obviously add my own macro and conditional compilation, but I note that _CHIBIOS_HAL_ is defined when using the HAL, and streams that contain chsnprintf is part of the HAL.

Problem is that this macro seems to be defined in the hal.h file so you can't use _CHIBIOS_HAL_ macro to conditionally compile the hal.h and associated headers that don't exist for non-ChibiOS builds.

I had expected there to be some macro defined in the makefiles specific to ChibiOS, which would then allow me to conditionally pull in the relevant ChibiIOS-specifics header files like hal.h and also pick up chsnprintf instead of snprintf but I can't see how to do that.

Thoughts anyone on the elegant way to do this, before I just go adding my own specific PREPROC define in the makefile and look for that in my code....?

User avatar
Site Admin
Posts: 13527
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 861 times
Been thanked: 743 times

Re: Detect compilation for ChibiOS

Postby Giovanni » Thu Oct 15, 2020 10:46 am


There are no macros defined into the makefile, not all implementations use makefiles, you may add it yourself using -D


Posts: 152
Joined: Wed Jan 09, 2019 12:37 pm
Has thanked: 19 times
Been thanked: 23 times

Re: Detect compilation for ChibiOS

Postby mikeprotts » Thu Oct 15, 2020 2:21 pm

Not entirely elegant, but would this work, coding everything as chsnprintf etc and:

#if !defined(chsnprintf)
#define chsnprintf snprintf

or maybe use your own mysnprintf that maps as:

#if defined(chsnprintf)
#define myprintf chsnprintf
#define myprintf snprintf

or else use a prefix (e.g. MYPREFIXsnprintf) with:
#if defined(chsnprintf)
#define MYPREFIX ch
#define MYPREFIX


Return to “General Support”

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests