Dynamic loading of external modules in program C?

I am sure that this problem has been resolved before, and I am curious how this is done. I have a code in which at startup I want to scan the contents of a directory and load the functionality.

In particular, I work with a scripting engine that I want to be able to add function calls. I want the main engine to provide very limited functionality. The user should be able to add additional functions through third-party libraries, which I want the engine to scan and load. How it's done?

+3
source share
3 answers

It depends on the platform. On win32, you call the LoadLibraryDLL to load, and then you get functions from it with GetProcAddress. On Unixy platforms, the equivalents are: dlopenand dlsym.

+6
source

You can use the POSIX functions dlopen / dlsym / dlerror / dlclose on Linux / UNIX to dynamically open shared libraries and access the symbols (including functions) that they provide, see man for details.

+4
source

If you want to use the library for this, I would recommend GLib (a utility library that is under the GTK + UI toolkit). It introduces the " " GModule ", which provides a clean, portable way to do this. It saves you from having to wrap the corresponding calls yourself, and also brings you the rest of GLib, which is very handy in C programs of any size.

+3
source

Source: https://habr.com/ru/post/1698398/


All Articles