library.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "library.h"
  4. #include "sysinfo.h"
  5. #include "filename.h"
  6. ////////////////////////////////////////////////////////////////////////////////
  7. /// Windows Platform
  8. ////////////////////////////////////////////////////////////////////////////////
  9. #if defined(__WIN32__)
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. namespace embree
  13. {
  14. /* opens a shared library */
  15. lib_t openLibrary(const std::string& file)
  16. {
  17. std::string fullName = file+".dll";
  18. FileName executable = getExecutableFileName();
  19. HANDLE handle = LoadLibrary((executable.path() + fullName).c_str());
  20. return lib_t(handle);
  21. }
  22. /* returns address of a symbol from the library */
  23. void* getSymbol(lib_t lib, const std::string& sym) {
  24. return (void*)GetProcAddress(HMODULE(lib),sym.c_str());
  25. }
  26. /* closes the shared library */
  27. void closeLibrary(lib_t lib) {
  28. FreeLibrary(HMODULE(lib));
  29. }
  30. }
  31. #endif
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// Unix Platform
  34. ////////////////////////////////////////////////////////////////////////////////
  35. #if defined(__UNIX__)
  36. #include <dlfcn.h>
  37. namespace embree
  38. {
  39. /* opens a shared library */
  40. lib_t openLibrary(const std::string& file)
  41. {
  42. #if defined(__MACOSX__)
  43. std::string fullName = "lib"+file+".dylib";
  44. #else
  45. std::string fullName = "lib"+file+".so";
  46. #endif
  47. void* lib = dlopen(fullName.c_str(), RTLD_NOW);
  48. if (lib) return lib_t(lib);
  49. FileName executable = getExecutableFileName();
  50. lib = dlopen((executable.path() + fullName).c_str(),RTLD_NOW);
  51. if (lib == nullptr) {
  52. const char* error = dlerror();
  53. if (error) {
  54. THROW_RUNTIME_ERROR(error);
  55. } else {
  56. THROW_RUNTIME_ERROR("could not load library "+executable.str());
  57. }
  58. }
  59. return lib_t(lib);
  60. }
  61. /* returns address of a symbol from the library */
  62. void* getSymbol(lib_t lib, const std::string& sym) {
  63. return dlsym(lib,sym.c_str());
  64. }
  65. /* closes the shared library */
  66. void closeLibrary(lib_t lib) {
  67. dlclose(lib);
  68. }
  69. }
  70. #endif