library.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "library.h"
  17. #include "sysinfo.h"
  18. #include "filename.h"
  19. ////////////////////////////////////////////////////////////////////////////////
  20. /// Windows Platform
  21. ////////////////////////////////////////////////////////////////////////////////
  22. #if defined(__WIN32__)
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. namespace embree
  26. {
  27. /* opens a shared library */
  28. lib_t openLibrary(const std::string& file)
  29. {
  30. std::string fullName = file+".dll";
  31. FileName executable = getExecutableFileName();
  32. HANDLE handle = LoadLibrary((executable.path() + fullName).c_str());
  33. return lib_t(handle);
  34. }
  35. /* returns address of a symbol from the library */
  36. void* getSymbol(lib_t lib, const std::string& sym) {
  37. return GetProcAddress(HMODULE(lib),sym.c_str());
  38. }
  39. /* closes the shared library */
  40. void closeLibrary(lib_t lib) {
  41. FreeLibrary(HMODULE(lib));
  42. }
  43. }
  44. #endif
  45. ////////////////////////////////////////////////////////////////////////////////
  46. /// Unix Platform
  47. ////////////////////////////////////////////////////////////////////////////////
  48. #if defined(__UNIX__)
  49. #include <dlfcn.h>
  50. namespace embree
  51. {
  52. /* opens a shared library */
  53. lib_t openLibrary(const std::string& file)
  54. {
  55. #if defined(__MACOSX__)
  56. std::string fullName = "lib"+file+".dylib";
  57. #else
  58. std::string fullName = "lib"+file+".so";
  59. #endif
  60. void* lib = dlopen(fullName.c_str(), RTLD_NOW);
  61. if (lib) return lib_t(lib);
  62. FileName executable = getExecutableFileName();
  63. lib = dlopen((executable.path() + fullName).c_str(),RTLD_NOW);
  64. if (lib == nullptr) {
  65. const char* error = dlerror();
  66. if (error) {
  67. THROW_RUNTIME_ERROR(error);
  68. } else {
  69. THROW_RUNTIME_ERROR("could not load library "+executable.str());
  70. }
  71. }
  72. return lib_t(lib);
  73. }
  74. /* returns address of a symbol from the library */
  75. void* getSymbol(lib_t lib, const std::string& sym) {
  76. return dlsym(lib,sym.c_str());
  77. }
  78. /* closes the shared library */
  79. void closeLibrary(lib_t lib) {
  80. dlclose(lib);
  81. }
  82. }
  83. #endif