Support C API dynamic loading using dlsym()#218
Merged
li-feng-sc merged 6 commits intoFeb 9, 2026
Conversation
Contributor
|
I wonder if you could add an abstraction layer on top of dlopen/dlsym. This way the generated code won't be tied to platforms. |
Author
|
Hi @li-feng-sc I have updated the PR to add an abstraction around |
Contributor
|
@kevinthornberry I mean something like this: class DynamicLibrary {
public:
using Handle = void*;
DynamicLibrary() = default;
explicit DynamicLibrary(const char* path) {
open(path);
}
~DynamicLibrary() {
close();
}
DynamicLibrary(const DynamicLibrary&) = delete;
DynamicLibrary& operator=(const DynamicLibrary&) = delete;
DynamicLibrary(DynamicLibrary&& other) noexcept
: handle_(std::exchange(other.handle_, nullptr)) {}
DynamicLibrary& operator=(DynamicLibrary&& other) noexcept {
if (this != &other) {
close();
handle_ = std::exchange(other.handle_, nullptr);
}
return *this;
}
void open(const char* path) {
close();
handle_ = ::dlopen(path, RTLD_NOW);
}
void close() noexcept {
if (handle_) {
::dlclose(handle_);
handle_ = nullptr;
}
}
bool isOpen() const noexcept {
return handle_ != nullptr;
}
template <typename T>
T tryGet(const char* symbol) const noexcept {
return isOpen() ? reinterpret_cast<T>(::dlsym(handle_, symbol)) : nullptr;
}
private:
Handle handle_ = nullptr;
};And usage: DynamicLibrary lib("libm.so.6");
auto cos_fn = lib.tryGet<double(*)(double)>("cos");
double v = cos_fn(0.5);With this library utility, the codegen can simply include a header file and emit portable code. |
li-feng-sc
approved these changes
Feb 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently the C wrapper classes link directly to the underlying C functions, meaning the library must be present at build-time. Add support for lazy-loading the C functions at runtime using
dlsym(), enabled with a new--c-wrapper-use-dlsym=truegenerator argument.Example djinni (namespace
test):C wrapper functions before:
C wrapper functions after:
DJINNI_C_LOAD_SYM_FUNC(libHandle, functionName)can be defined by the platform to provide a symbol loader function similar todlsym(). If it is not explicitly defineddlsym()will be used by default.DJINNI_C_LIB_HANDLE_test_Foocan be defined to reference a specific handle opened withDJINNI_C_LOAD_SYM_FUNC(). AlsoDJINNI_C_DEFAULT_LIB_HANDLEcan be defined as a global default (individual wrapper classes can still use their own definitions overriding the default). If neither of these is definedRTLD_DEFAULTwill be used as the handle.If the
--c-wrapper-use-dlsym=trueargument is not supplied, the generated code is identical to the current version.