lib.odin 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package dynlib
  2. /*
  3. A handle to a dynamically loaded library.
  4. */
  5. Library :: distinct rawptr
  6. /*
  7. Loads a dynamic library from the filesystem. The paramater `global_symbols` makes the symbols in the loaded
  8. library available to resolve references in subsequently loaded libraries.
  9. The paramater `global_symbols` is only used for the platforms `linux`, `darwin`, `freebsd` and `openbsd`.
  10. On `windows` this paramater is ignored.
  11. The underlying behaviour is platform specific.
  12. On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlopen`.
  13. On `windows` refer to `LoadLibraryW`.
  14. **Implicit Allocators**
  15. `context.temp_allocator`
  16. Example:
  17. import "core:dynlib"
  18. import "core:fmt"
  19. load_my_library :: proc() {
  20. LIBRARY_PATH :: "my_library.dll"
  21. library, ok := dynlib.load_library(LIBRARY_PATH)
  22. if ! ok {
  23. return
  24. }
  25. fmt.println("The library %q was successfully loaded", LIBRARY_PATH)
  26. }
  27. */
  28. load_library :: proc(path: string, global_symbols := false) -> (library: Library, did_load: bool) {
  29. return _load_library(path, global_symbols)
  30. }
  31. /*
  32. Unloads a dynamic library.
  33. The underlying behaviour is platform specific.
  34. On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlclose`.
  35. On `windows` refer to `FreeLibrary`.
  36. Example:
  37. import "core:dynlib"
  38. import "core:fmt"
  39. load_then_unload_my_library :: proc() {
  40. LIBRARY_PATH :: "my_library.dll"
  41. library, ok := dynlib.load_library(LIBRARY_PATH)
  42. if ! ok {
  43. return
  44. }
  45. did_unload := dynlib.unload_library(library)
  46. if ! did_unload {
  47. return
  48. }
  49. fmt.println("The library %q was successfully unloaded", LIBRARY_PATH)
  50. }
  51. */
  52. unload_library :: proc(library: Library) -> (did_unload: bool) {
  53. return _unload_library(library)
  54. }
  55. /*
  56. Loads the address of a procedure/variable from a dynamic library.
  57. The underlying behaviour is platform specific.
  58. On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlsym`.
  59. On `windows` refer to `GetProcAddress`.
  60. **Implicit Allocators**
  61. `context.temp_allocator`
  62. Example:
  63. import "core:dynlib"
  64. import "core:fmt"
  65. find_a_in_my_library :: proc() {
  66. LIBRARY_PATH :: "my_library.dll"
  67. library, ok := dynlib.load_library(LIBRARY_PATH)
  68. if ! ok {
  69. return
  70. }
  71. a, found_a := dynlib.symbol_address(library, "a")
  72. if found_a do fmt.printf("The symbol %q was found at the address %v", "a", a)
  73. }
  74. */
  75. symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) #optional_ok {
  76. return _symbol_address(library, symbol)
  77. }