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

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dlfcn.h>
  4.  
  5. int
  6. thingy()
  7. {
  8. return 42;
  9. }
  10.  
  11. int
  12. main(int argc, char **argv)
  13. {
  14. const char *module = NULL;
  15. const char *symbol = NULL;
  16. void *pfn = NULL, *handle = NULL;
  17.  
  18. if (argc < 2 || argc > 3) {
  19. printf("usage: dltest executablefile symbol\n");
  20. return 1;
  21. } else if (argc == 2) {
  22. symbol = argv[1];
  23. module = argv[0];
  24. } else {
  25. module = argv[1];
  26. symbol = argv[2];
  27. }
  28.  
  29. if (module != NULL) {
  30. handle = dlopen(module, RTLD_LAZY);
  31. if (handle == NULL) {
  32. perror("dlopen");
  33. return 1;
  34. }
  35. }
  36. pfn = dlsym(handle, symbol);
  37. if (pfn != NULL) {
  38. printf("Found symbols.\n");
  39. }
  40. if (module != NULL) {
  41. dlclose(handle);
  42. }
  43. return 0;
  44. }