12 #ifndef _CUDA_DYNAMIC_LOADING_DYNAMIC_LOADING_H_
13 #define _CUDA_DYNAMIC_LOADING_DYNAMIC_LOADING_H_
20 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && \
23 #elif defined(__unix__) || defined(__unix) || \
24 (defined(__APPLE__) && defined(__MACH__))
27 #error "Unknown compiler"
85 #if defined(__unix__) || defined(__unix) || \
86 (defined(__APPLE__) && defined(__MACH__))
97 static void* get_library_handle_unix(
const char* lib_name)
99 void* handle = dlopen(lib_name, RTLD_LAZY);
103 throw std::runtime_error(dlerror());
115 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && \
127 static HMODULE get_library_handle_windows(
const char* lib_name)
129 HMODULE handle = LoadLibrary(TEXT(lib_name));
133 std::ostringstream oss;
134 oss <<
"Cannot load the shared library '" << lib_name <<
"'." \
136 std::string message = oss.str();
137 throw std::runtime_error(message);
163 template <
typename Signature>
165 const char* lib_name,
166 const char* symbol_name)
168 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && \
171 HMODULE handle = get_library_handle_windows(lib_name);
172 FARPROC symbol = GetProcAddress(handle, symbol_name);
176 std::ostringstream oss;
177 oss <<
"The symbol '" << symbol <<
"' is failed to load " \
178 <<
"from the shared library '" << lib_name <<
"'." \
180 std::string message = oss.str();
181 throw std::runtime_error(message);
184 #elif defined(__unix__) || defined(__unix) || \
185 (defined(__APPLE__) && defined(__MACH__))
187 void* handle = get_library_handle_unix(lib_name);
188 void* symbol = dlsym(handle, symbol_name);
190 char *error = dlerror();
193 throw std::runtime_error(dlerror());
197 #error "Unknown compiler"
201 return reinterpret_cast<Signature
>(symbol);
Dynamic loading of shared libraries using dlopen tool.
Signature load_symbol(const char *lib_name, const char *symbol_name)
Loads a symbol within a library and returns a pointer to the symbol (function pointer).