CodeBlanks Logo Icon CodeBlanks

Summary - Dynamic loading

Combine dlopen and dlsym to write a plugin system.

Dynamic loading summary

You learned the dynamic loading toolkit: dlopen loads a library, dlsym resolves a symbol, dlclose closes, dlerror catches errors. These are the core of plugin systems, interpreters and modular architecture.

In this summary we will combine the four tools into a dl preparation app. Using them together shows how a real plugin system is built. The output is deterministic: we print a fixed ready message.

Practice Prepare dl

Clear the old error with dlerror();. Load with void *h = dlopen(NULL, RTLD_LAZY);. Get the symbol with void *sym = NULL; if (h) { sym = dlsym(h, "printf"); }. (void)sym;. Print with printf("dl ready\n");. Close with if (h) dlclose(h);.

Starter code

main.c
#include <stdio.h>
#include <dlfcn.h>

// Clear old error and load
// Resolve symbol
// Print ready and close

int main(void) {
    return 0;
}

Sample solution

main.c
#include <stdio.h>
#include <dlfcn.h>

int main(void) {
    dlerror();
    void *h = dlopen(NULL, RTLD_LAZY);
    void *sym = NULL;
    if (h) {
        sym = dlsym(h, "printf");
    }
    (void)sym;
    printf("dl ready\n");
    if (h) dlclose(h);
    return 0;
}

Expected output

dl ready

Next steps

You completed dynamic loading. These foundations are the core of plugin systems, scripting language interpreters and modular service architecture. Load with dlopen, call with dlsym, guard with dlerror.

Next topic: cryptography basics. You will learn data integrity and confidentiality with hash, HMAC, AES and secure random numbers. Combined with dynamic loading, you can for instance load OpenSSL at runtime and use it.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which function should be called first?

  • dlclose
  • dlopen Correct answer
  • dlsym
  • dlerror

Explanation: dlopen is called first; then dlsym, finally dlclose.

2 Match dl functions

Term Meaning
dlopen Loads
dlsym Resolves
dlclose Closes
dlerror Errors

All lessons in Advanced