PathEngine home previous: Linking with the TestBednext: Memory Allocation Hooks
Contents, Programmers Guide, Linking with the SDK, Dynamic Linking on GCC Based Platforms

Dynamic Linking on GCC Based Platforms

The GCC based build setup generates a .so file instead of a .dll.
This is essentially a Linux equivalent to a Windows dll.

The following code shows how PathEngine can be loaded as a .so, and an entry point obtained:

#include "i_pathengine.h"
#include "PathEngine_DirectLinkage.h"
#include <dlfcn.h>
#include <stdio.h>
#include <signal.h>

class cErrorHandler : public iErrorHandler
{
public:
    eAction handle(const char* type, const char* description, const char *const* attributes)
    {
        printf("Error handler called:\n");
        printf(type);
        printf("\n");
        printf(description);
        printf("\n");
        raise(SIGTRAP);
        return CONTINUE;
    }
};

cErrorHandler gErrorHandler;

int
main (int argc, char* argv[])
{
    const char* errorString;

    void* handle = dlopen("./libPathEngine.so", RTLD_NOW);
    if(!handle)
    {
        printf("!handle\n");
        errorString = dlerror();
        if(errorString)
        {
            printf(errorString);
        }
        return 1;
    }

    iPathEngine* (*PathEngine_InitialiseAndObtainRootInterface_Pointer)(iErrorHandler*);
    void (*PathEngine_ShutDown_Pointer)();

    dlerror();
    *(void **)(&PathEngine_InitialiseAndObtainRootInterface_Pointer) = dlsym(handle, "PathEngine_InitialiseAndObtainRootInterface");
    errorString = dlerror();
    if(errorString)
    {
        printf(errorString);
        printf("\n");
        dlclose(handle);
        return 1;
    }
    *(void **)(&PathEngine_ShutDown_Pointer) = dlsym(handle, "PathEngine_ShutDown");
    errorString = dlerror();
    if(errorString)
    {
        printf(errorString);
        printf("\n");
        dlclose(handle);
        return 1;
    }

    iPathEngine* pathEngine = (*PathEngine_InitialiseAndObtainRootInterface_Pointer)(&gErrorHandler);

    if(pathEngine->getInterfaceMajorVersion() != PATHENGINE_INTERFACE_MAJOR_VERSION
        ||
        pathEngine->getInterfaceMinorVersion() < PATHENGINE_INTERFACE_MINOR_VERSION)
    {
        gErrorHandler.handle("Fatal", "LoadPathEngine: pathengine version is incompatible with headers used for compilation.", 0);
        return false;
    }

// *** do things with the API here



    (*PathEngine_ShutDown_Pointer);

    int error = dlclose(handle);
    if(error)
    {
        printf("error from dclose()\n");
        return 1;
    }
    return 0;
}

In order to make these dll calls your application is likely to need an additional '-ldl' linker switch.
(This and other details for the exact setup to use for shared object linkage may of course vary slightly between different Linux distributions, and across other platforms where a GCC build setup is used.)

Example project

The 'LoadPathEngine' example project shows this linkage in action.


Documentation for PathEngine release 6.00 - Copyright © 2002-2016 PathEnginenext: Memory Allocation Hooks