DynamicLoader.hxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #ifndef cmsys_DynamicLoader_hxx
  4. #define cmsys_DynamicLoader_hxx
  5. #include <cmsys/Configure.hxx>
  6. #include <string>
  7. #if defined(__hpux)
  8. #include <dl.h>
  9. #elif defined(_WIN32) && !defined(__CYGWIN__)
  10. #include <windows.h>
  11. #elif defined(__APPLE__)
  12. #include <AvailabilityMacros.h>
  13. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  14. #include <mach-o/dyld.h>
  15. #endif
  16. #elif defined(__BEOS__)
  17. #include <be/kernel/image.h>
  18. #endif
  19. namespace cmsys {
  20. /** \class DynamicLoader
  21. * \brief Portable loading of dynamic libraries or dll's.
  22. *
  23. * DynamicLoader provides a portable interface to loading dynamic
  24. * libraries or dll's into a process.
  25. *
  26. * Directory currently works with Windows, Apple, HP-UX and Unix (POSIX)
  27. * operating systems
  28. *
  29. * \warning dlopen on *nix system works the following way:
  30. * If filename contains a slash ("/"), then it is interpreted as a (relative
  31. * or absolute) pathname. Otherwise, the dynamic linker searches for the
  32. * library as follows : see ld.so(8) for further details):
  33. * Whereas this distinction does not exist on Win32. Therefore ideally you
  34. * should be doing full path to guarantee to have a consistent way of dealing
  35. * with dynamic loading of shared library.
  36. *
  37. * \warning the Cygwin implementation do not use the Win32 HMODULE. Put extra
  38. * condition so that we can include the correct declaration (POSIX)
  39. */
  40. class cmsys_EXPORT DynamicLoader
  41. {
  42. public:
  43. // Ugly stuff for library handles
  44. // They are different on several different OS's
  45. #if defined(__hpux)
  46. typedef shl_t LibraryHandle;
  47. #elif defined(_WIN32) && !defined(__CYGWIN__)
  48. typedef HMODULE LibraryHandle;
  49. #elif defined(__APPLE__)
  50. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  51. typedef NSModule LibraryHandle;
  52. #else
  53. typedef void* LibraryHandle;
  54. #endif
  55. #elif defined(__BEOS__)
  56. typedef image_id LibraryHandle;
  57. #else // POSIX
  58. typedef void* LibraryHandle;
  59. #endif
  60. // Return type from DynamicLoader::GetSymbolAddress.
  61. typedef void (*SymbolPointer)();
  62. /** Load a dynamic library into the current process.
  63. * The returned LibraryHandle can be used to access the symbols in the
  64. * library. */
  65. static LibraryHandle OpenLibrary(const std::string&);
  66. /** Attempt to detach a dynamic library from the
  67. * process. A value of true is returned if it is successful. */
  68. static int CloseLibrary(LibraryHandle);
  69. /** Find the address of the symbol in the given library. */
  70. static SymbolPointer GetSymbolAddress(LibraryHandle, const std::string&);
  71. /** Return the default module prefix for the current platform. */
  72. static const char* LibPrefix() { return "lib"; }
  73. /** Return the default module suffix for the current platform. */
  74. static const char* LibExtension() { return ".so"; }
  75. /** Return the last error produced from a calls made on this class. */
  76. static const char* LastError();
  77. }; // End Class: DynamicLoader
  78. } // namespace cmsys
  79. #endif