Skip to content

Commit 17a5c7c

Browse files
committed
Add util_open_library, util_close_library, util_get_symbol_addr
Add util_open_library(), util_close_library() and util_get_symbol_addr() to utils_load_library.h Including this header forces linking with libdl on Linux. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent a01d06f commit 17a5c7c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/utils/utils_load_library.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
/*
11+
* Including this header forces linking with libdl on Linux.
12+
*/
13+
14+
#ifndef UMF_LOAD_LIBRARY_H
15+
#define UMF_LOAD_LIBRARY_H 1
16+
17+
#ifdef _WIN32 /* Windows */
18+
19+
#include <windows.h>
20+
21+
#include <libloaderapi.h>
22+
23+
#else /* Linux */
24+
25+
#include <dlfcn.h> // forces linking with libdl on Linux
26+
27+
#endif /* _WIN32 */
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
#ifdef _WIN32 /* Windows */
34+
35+
static inline void *util_open_library(const char *filename) {
36+
return LoadLibrary(TEXT(filename));
37+
}
38+
39+
static inline int util_close_library(void *handle) {
40+
// If the FreeLibrary function succeeds, the return value is nonzero.
41+
// If the FreeLibrary function fails, the return value is zero.
42+
return (FreeLibrary(handle) == 0);
43+
}
44+
45+
static inline void *util_get_symbol_addr(void *handle, const char *symbol) {
46+
return GetProcAddress(handle, symbol);
47+
}
48+
49+
#else /* Linux */
50+
51+
static inline void *util_open_library(const char *filename) {
52+
return dlopen(filename, RTLD_LAZY);
53+
}
54+
55+
static inline int util_close_library(void *handle) { return dlclose(handle); }
56+
57+
static inline void *util_get_symbol_addr(void *handle, const char *symbol) {
58+
return dlsym(handle, symbol);
59+
}
60+
61+
#endif /* _WIN32 */
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif /* UMF_LOAD_LIBRARY_H */

0 commit comments

Comments
 (0)