How do I load shared libraries at runtime on macOS in C++?

Loading shared libraries at runtime in macOS using C++ can be accomplished through the use of the dynamic linking library APIs provided by the system. The main functions you'll use are dlopen, dlsym, and dlclose. This allows a program to load a shared library (dynamic library) at runtime, rather than at compile time.

Here’s a simple example to demonstrate how to load a shared library and call a function from it:

#include <iostream> #include <dlfcn.h> typedef void (*func_t)(); // Define a type for the function we want to call int main() { // Load the shared library void* handle = dlopen("libmylibrary.dylib", RTLD_LAZY); if (!handle) { std::cerr << "Cannot open library: " << dlerror() << std::endl; return 1; } // Reset errors dlerror(); // Load the symbol (function) from the library func_t myFunction = (func_t) dlsym(handle, "myFunction"); const char* dlsym_error = dlerror(); if (dlsym_error) { std::cerr << "Cannot load symbol 'myFunction': " << dlsym_error << std::endl; dlclose(handle); return 1; } // Call the function myFunction(); // Close the library dlclose(handle); }

macOS C++ shared libraries dynamic linking runtime library loading