/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include<dlfcn.h>
...
array return_type functionName(int array_dim1, int array_dim2, 
                           data_type1 arg1, data_type2 arg2) [:][:] {
  void *dlhandle, *fptr;
  array return_type retval[array_dim1][array_dim2];

  /* load the dynamically loaded library */
  dlhandle = dlopen("libproject.dl", RTLD_LAZY);
  if(dlhandle == NULL) {
    fprintf(_stderr, "Error: %s(): dlopen(): %s\n", 
            __func__, dlerror());
    return FAIL_VALUE; /* FAIL_VALUE is typically NULL for point
                          and negative value for integral type */
  }

  /* get the address by function name */
  fptr = dlsym(dlhandle, "functionName_chdl");
  if(fptr == NULL) {
    printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
    return FAIL_VALUE;
  }

  /* Address of the computational array is passed as an argument 
     to C address space.
     To avoid checking prototype, the third argument should be NULL */
  dlrunfun(fptr, NULL, NULL, &retval[0][0], 
           array_dim1, array_dim2, arg1, arg2);

  /* close the dynamically loaded library */
  if(dlclose(handle)!=0) {
    fprintf(_stderr, "Error: %s(): dlclose(): %s\n", 
           __func__, dlerror());
    return FAIL_VALUE;
  }

  return retval;
}

