Detect if mbed OS (mbed 3) or mbed classic (mbed 2)

Is there a way for me to detect with the preprocessor whether the code is being compiled with mbed OS or mbed classic libraries? Or do I have to have a separate code base for the two platforms if I want to support both?

I’ve seen the macro MBED_LIBRARY_VERSION, but I can’t find any documentation on what the number represents. My current mbed classic library source has MBED_LIBRARY_VERSION=111 and my mbed OS library source has MBED_LIBRARY_VERSION=91, which isn’t self-explanatory.

Try this:

// mbed OS
// not mbed OS
1 Like

Great, thanks! That works perfectly! I see that comes from yotta_config.h, which is generated from the module.json and other configuration files. Is there any documentation or other assurance that this method will work in future releases of Yotta and mbed OS?

Never mind: I decided on an alternative solution. One of the reasons I need to detect whether I’m compiling for mbed OS is so I can know whether to #include “mbed.h” or “mbed-drivers/mbed.h”. However, YOTTA_CFG_MBED_OS isn’t available before #including anything. So instead, I’m adding a line to my custom Cmake file (that is only used for mbed OS) and defining my own macro that I can use:

target_compile_definitions(my-module PRIVATE MY_PROJECT_MBED_OS=1)

The #ifdef that I referenced is what we use in BLE_API, which is one of the stable core APIs of both mbed and mbed OS. So I guess it’ll be around for a while :slight_smile:

Ok, I see that! But what I don’t see in the BLE_API code, per my last comment: how do you guarantee that YOTTA_CFG_MBED_OS is defined before you include mbed.h? YOTTA_CFG_MBED_OS is defined in yotta_config.h, which is included somehow when I #include mbed.h. If I try to condition #include mbed.h on YOTTA_CFG_MBED_OS, I get the wrong behavior (on mbed OS) unless I included mbed.h earlier. How do you work around that?