Path: blob/master/thirdparty/embree/common/sys/library.cpp
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#include "library.h"4#include "sysinfo.h"5#include "filename.h"67////////////////////////////////////////////////////////////////////////////////8/// Windows Platform9////////////////////////////////////////////////////////////////////////////////1011#if defined(__WIN32__)1213#define WIN32_LEAN_AND_MEAN14#include <windows.h>1516namespace embree17{18/* opens a shared library */19lib_t openLibrary(const std::string& file)20{21std::string fullName = file+".dll";22FileName executable = getExecutableFileName();23HANDLE handle = LoadLibrary((executable.path() + fullName).c_str());24return lib_t(handle);25}2627/* returns address of a symbol from the library */28void* getSymbol(lib_t lib, const std::string& sym) {29return (void*)GetProcAddress(HMODULE(lib),sym.c_str());30}3132/* closes the shared library */33void closeLibrary(lib_t lib) {34FreeLibrary(HMODULE(lib));35}36}37#endif3839////////////////////////////////////////////////////////////////////////////////40/// Unix Platform41////////////////////////////////////////////////////////////////////////////////4243#if defined(__UNIX__)4445#include <dlfcn.h>4647namespace embree48{49/* opens a shared library */50lib_t openLibrary(const std::string& file)51{52#if defined(__MACOSX__)53std::string fullName = "lib"+file+".dylib";54#else55std::string fullName = "lib"+file+".so";56#endif57void* lib = dlopen(fullName.c_str(), RTLD_NOW);58if (lib) return lib_t(lib);59FileName executable = getExecutableFileName();60lib = dlopen((executable.path() + fullName).c_str(),RTLD_NOW);61if (lib == nullptr) {62const char* error = dlerror();63if (error) {64THROW_RUNTIME_ERROR(error);65} else {66THROW_RUNTIME_ERROR("could not load library "+executable.str());67}68}69return lib_t(lib);70}7172/* returns address of a symbol from the library */73void* getSymbol(lib_t lib, const std::string& sym) {74return dlsym(lib,sym.c_str());75}7677/* closes the shared library */78void closeLibrary(lib_t lib) {79dlclose(lib);80}81}82#endif838485