Checking, at runtime, for existence of a function in a program
-
21-08-2019 - |
Question
I am writing a program that is meant to be extended by some function definitions. One of the way of compiling the program is to create a single executable linking your code to the main code. The problem is: one of the function to define is optional and I need to test for that.
On Linux, here is what I am doing:
Compile the program with the "-rdynamic" option to g++ or "--export-dynamic" option to ld. Then, Use ldsym like this:
fct_type myfct = (fct_type)dlsym(RTLD_DEFAULT, "fct");
If the function "fct" exists in the program, this will return its address, otherwise, it returns NULL.
Now, on Windows, I am supposed to be able to do so:
dll_handle = GetModuleHandle(0);
fct_type myfct = (fct_type)GetProcAddress(dll_handle, "fct");
But there is no "-rdynamic" or "--export-dynamic" option to g++ on MinGW32! So this doesn't work. Does anybody knows what to do on windows with MinGW32 ?
Solution
Ok, so in the end, I will answer my own question ...
You have to link with the flag -Wl,--export-all-symbols
and it works ...