DynamicLoader.hxx.in 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 @KWSYS_NAMESPACE@_DynamicLoader_hxx
  4. #define @KWSYS_NAMESPACE@_DynamicLoader_hxx
  5. #include <@KWSYS_NAMESPACE@/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 @KWSYS_NAMESPACE@ {
  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 @KWSYS_NAMESPACE@_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. enum OpenFlags
  63. {
  64. // Search for dependent libraries beside the library being loaded.
  65. //
  66. // This is currently only supported on Windows.
  67. SearchBesideLibrary = 0x00000001,
  68. AllOpenFlags = SearchBesideLibrary
  69. };
  70. /** Load a dynamic library into the current process.
  71. * The returned LibraryHandle can be used to access the symbols in the
  72. * library. The optional second argument is a set of flags to use when
  73. * opening the library. If unrecognized or unsupported flags are specified,
  74. * the library is not opened. */
  75. static LibraryHandle OpenLibrary(const std::string&);
  76. static LibraryHandle OpenLibrary(const std::string&, int);
  77. /** Attempt to detach a dynamic library from the
  78. * process. A value of true is returned if it is successful. */
  79. static int CloseLibrary(LibraryHandle);
  80. /** Find the address of the symbol in the given library. */
  81. static SymbolPointer GetSymbolAddress(LibraryHandle, const std::string&);
  82. /** Return the default module prefix for the current platform. */
  83. static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; }
  84. /** Return the default module suffix for the current platform. */
  85. static const char* LibExtension() { return "@KWSYS_DynamicLoader_SUFFIX@"; }
  86. /** Return the last error produced from a calls made on this class. */
  87. static const char* LastError();
  88. }; // End Class: DynamicLoader
  89. } // namespace @KWSYS_NAMESPACE@
  90. #endif