Native Extensions

A quick note on supporting multiple platforms in your extensions.

When supporting cross platform code, I often use one of these tricks to catch “errors”.

Compile error

If I don’t support a certain platform, and if there is no reason or good way to hide it. I make sure that I get a clear error message when compiling:

#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_ANDROID)
    // my working code
#else
    #error "Platform not supported for this extension!"
#endif

Imho, it’s a LOT clearer than error LNK2019: unresolved external symbol <symbol> referenced in function...

Adding a “null” implementation

Sometimes is feels “nicer” to add a dummy, non working or mock implementation for those platforms not supported:

#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_ANDROID)
    // my working code
#else
// for the rest of the unsupported platforms
static dmExtension::Result AppInitializeExtension(dmExtension::AppParams* params)
{
    dmLogWarning("Registered %s (null) Extension\n", MODULE_NAME);
    return dmExtension::RESULT_OK;
}

static dmExtension::Result InitializeExtension(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

static dmExtension::Result AppFinalizeExtension(dmExtension::AppParams* params)
{
    return dmExtension::RESULT_OK;
}

static dmExtension::Result FinalizeExtension(dmExtension::Params* params)
{
    return dmExtension::RESULT_OK;
}

#endif

DM_DECLARE_EXTENSION(EXTENSION_NAME, LIB_NAME, AppInitializeExtension, AppFinalizeExtension, InitializeExtension, 0, 0, FinalizeExtension);

These two tricks will save yourself (and others) a lot of development and debugging time!

8 Likes