Code: Select all
//example
call_func("func_1");
call_func("func_2");
...
Code: Select all
//example
call_func("func_1");
call_func("func_2");
...
Dynamic linking.aladdin wrote: Is there a way to call a function at runtime knowing only its name? for example, by passing its name as a parametre to another function that will call itCode: Select all
//example call_func("func_1"); call_func("func_2"); ...
Code: Select all
void (*fp)(int, bool, char, char*) = dl_get("function");
*fp(0123, true, 'a', "abcdef");
I'm actually using this solution, but i don't like itForget about a general solution, instead use a special module format, where you'd have some code at the beginning of the module to check from some table whether it's already loaded, then return a table of functions it provides. If you keep names in that table, you get by name lookup of function pointers, which you can then call normally. You might need a second table for functions the module imports, which you fill in by querying the exports of other functions. This way you don't need to lookup a name every time you want to call a function, just once.
this is what I m planning to do in the future since i want to implement a fully modulable kernel.You can also do what Linux does: read normal .o files, which already contain such a table, parse that, and proceed as in the previous option. In this case you do the imports by "linking" the object into your running kernel.
I think this is a good solution, but it's not too important at the moment. coze this is used for libraries and what I want to do is only calling "internal modules" (compiled with the kernel).Finally, you could implement full dlopen/dlsym/dlclose if you want, and use something like ELF .so files, which is designed to to mostly what described above. A hack (such as that in Linux) for .o files might be easier though, but YMMV.