Dynamic Loading Overview
Source: qar-streaming-c/examples/dynamic_loading.c. See also Static linking vs. dynamic loading for the ABI rationale.
Dynamic Loading Overview
What You Will Learn
- Parse command-line arguments to locate the shared library
- Dynamically load qar-streaming-c at runtime
- Initialize and tear down the global library state
Prerequisites
- Build the SDK and locate the produced qar-streaming-c shared library
- Ensure runtime binaries sit next to the library (default assumption)
Build and Run
cmake --build --preset x64-windows-debug --target dynamic_loading
./build/x64-windows/Debug/dynamic_loading.exe <path-to-qar-streaming-c.dll> [runtime-dir]
Parse Arguments
Obtain the shared-library path and optional runtime directory.
if(argc < 2)
{
print_usage(argv[0]);
return 1;
}
const char* library_path = argv[1];
const char* runtime_dir = NULL;
char runtime_dir_buffer[1024] = { 0 };
if(argc >= 3)
{
runtime_dir = argv[2];
}
else if(get_dir_from_path(library_path, runtime_dir_buffer, sizeof(runtime_dir_buffer)))
{
runtime_dir = runtime_dir_buffer;
}
Load the Shared Library
Use qar_library_load to dynamically load the DLL/SO/Dylib.
printf("Loading qar-streaming-c from: %s\n", library_path);
if(!qar_library_load(library_path))
{
fprintf(stderr, "Failed to load '%s'. Ensure the path is correct.\n", library_path);
return 2;
}
Initialize and Shutdown
Initialize global state with qar_library_init, then destroy and unload when done.
QarLibraryInit library_init = qar_library_init_default();
library_init.enable_console_logging = true;
library_init.log_folder_path = NULL; // Console logging is sufficient for this tutorial
QarResult init_result = qar_library_init(&library_init);
if(qar_result_is_error(init_result))
{
log_result("qar_library_init", init_result);
return 3;
}
printf("Library initialized. Runtime binaries directory: %s\n", runtime_dir ? runtime_dir : "(derived from library path)");
QarResult destroy_result = qar_library_destroy();
log_result("qar_library_destroy", destroy_result);
printf("Library unloaded. Dynamic loading tutorial complete.\n");
Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.