aboutsummaryrefslogtreecommitdiff
path: root/core/dynlib/example
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-01-06 01:31:27 +0100
committerGitHub <noreply@github.com>2024-01-06 01:31:27 +0100
commitd6a89d667dc2dacdc760838cd489c3832b00b8f7 (patch)
treebff736a307abd3d30a0825e2b3efddcac24b58c6 /core/dynlib/example
parentb408ec6bac9ba417faf51a2364542e5d9493e193 (diff)
Add `dynlib.initialize_symbols` (#3071)
``` package example import "core:dynlib" import "core:fmt" Symbols :: struct { // `foo_` is prefixed, so we look for the symbol `foo_add`. add: proc "c" (int, int) -> int, // We use the tag here to override the symbol to look for, namely `bar_sub`. sub: proc "c" (int, int) -> int `dynlib:"bar_sub"`, // Exported global (if exporting an i32, the type must be ^i32 because the symbol is a pointer to the export.) // If it's not a pointer or procedure type, we'll skip the struct field. hellope: ^i32, // Handle to free library. // We can have more than one of these so we can match symbols for more than one DLL with one struct. _my_lib_handle: dynlib.Library, } main :: proc() { sym: Symbols // Load symbols from `lib.dll` into Symbols struct. // Each struct field is prefixed with `foo_` before lookup in the DLL's symbol table. // The library's Handle (to unload) will be stored in `sym._my_lib_handle`. This way you can load multiple DLLs in one struct. count := dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle") defer dynlib.unload_library(sym._my_lib_handle) fmt.printf("%v symbols loaded from lib.dll (%p).\n", count, sym._my_lib_handle) if count > 0 { fmt.println("42 + 42 =", sym.add(42, 42)) fmt.println("84 - 13 =", sym.sub(84, 13)) fmt.println("hellope =", sym.hellope^) } } ```
Diffstat (limited to 'core/dynlib/example')
-rw-r--r--core/dynlib/example/example.odin36
-rw-r--r--core/dynlib/example/lib.odin14
2 files changed, 50 insertions, 0 deletions
diff --git a/core/dynlib/example/example.odin b/core/dynlib/example/example.odin
new file mode 100644
index 000000000..7a6368d59
--- /dev/null
+++ b/core/dynlib/example/example.odin
@@ -0,0 +1,36 @@
+package example
+
+import "core:dynlib"
+import "core:fmt"
+
+Symbols :: struct {
+ // `foo_` is prefixed, so we look for the symbol `foo_add`.
+ add: proc "c" (int, int) -> int,
+ // We use the tag here to override the symbol to look for, namely `bar_sub`.
+ sub: proc "c" (int, int) -> int `dynlib:"bar_sub"`,
+
+ // Exported global (if exporting an i32, the type must be ^i32 because the symbol is a pointer to the export.)
+ // If it's not a pointer or procedure type, we'll skip the struct field.
+ hellope: ^i32,
+
+ // Handle to free library.
+ // We can have more than one of these so we can match symbols for more than one DLL with one struct.
+ _my_lib_handle: dynlib.Library,
+}
+
+main :: proc() {
+ sym: Symbols
+
+ // Load symbols from `lib.dll` into Symbols struct.
+ // Each struct field is prefixed with `foo_` before lookup in the DLL's symbol table.
+ // The library's Handle (to unload) will be stored in `sym._my_lib_handle`. This way you can load multiple DLLs in one struct.
+ count := dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle")
+ defer dynlib.unload_library(sym._my_lib_handle)
+ fmt.printf("%v symbols loaded from lib.dll (%p).\n", count, sym._my_lib_handle)
+
+ if count > 0 {
+ fmt.println("42 + 42 =", sym.add(42, 42))
+ fmt.println("84 - 13 =", sym.sub(84, 13))
+ fmt.println("hellope =", sym.hellope^)
+ }
+} \ No newline at end of file
diff --git a/core/dynlib/example/lib.odin b/core/dynlib/example/lib.odin
new file mode 100644
index 000000000..25687a653
--- /dev/null
+++ b/core/dynlib/example/lib.odin
@@ -0,0 +1,14 @@
+package library
+
+@(export)
+foo_add :: proc "c" (a, b: int) -> (res: int) {
+ return a + b
+}
+
+@(export)
+bar_sub :: proc "c" (a, b: int) -> (res: int) {
+ return a - b
+}
+
+@(export)
+foo_hellope: i32 = 42 \ No newline at end of file