Posted to tcl by patthoyts at Thu Dec 21 14:37:39 GMT 2006view pretty

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int
thingy()
{
  return 42;
}

int
main(int argc, char **argv)
{
  const char *module = NULL;
  const char *symbol = NULL;
  void *pfn = NULL, *handle = NULL;

  if (argc < 2 || argc > 3) {
    printf("usage: dltest executablefile symbol\n");
    return 1;
  } else if (argc == 2) {
    symbol = argv[1];
    module = argv[0];
  } else {
    module = argv[1];
    symbol = argv[2];
  }

  if (module != NULL) {
    handle = dlopen(module, RTLD_LAZY);
    if (handle == NULL) {
      perror("dlopen");
      return 1;
    }
  }
  pfn = dlsym(handle, symbol);
  if (pfn != NULL) {
    printf("Found symbols.\n");
  }
  if (module != NULL) {
    dlclose(handle);
  }
  return 0;
}