I have written a code where in it would take in a executable file and the [lib*.so] library as my arguments and link @ Run-time.
I want to also take in the function in the (*.o) file @ run-time and link it. But I don't know how to go about it.
EDIT 1: The function I'm trying to link is a part of the .o file in the lib*.so library. So I want to specify the library name and also the function name which is in the same library @ Run-Time.
For eg. If my library contains two functions(i.e *.o files) the linker should compile the function which I want to use @Run-Time.
I have posted the code,please help :
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h> // use -ldl
typedef float (*DepFn)(short, short);
int main(int argc, char* argv[])
{
void* lib;
DepFn df;
if(argc < 2)
return printf("USAGE: %s lib-file\n", argv[0]);
lib = dlopen(argv[1], RTLD_NOW);
if(lib == NULL)
return printf("ERROR: Cannot load library\n");
df = dlsym(lib, "Depreciation");
if(df)
{
short l, i;
printf("Enter useful-life of asset: ");
scanf("%hd", &l);
for(i = 1; i <= l; i++)
{
float d = 100 * df(l, i);
printf("%hd\t%.1f%%\n", i, d);
}
}
else
printf("ERROR: Invalid library\n");
dlclose(lib);
}