I want to create a program that reads a static library using dlfcn.h, in Main.c, there will be a variable called a which is global, and I want to access it through the library using extern int a and using it in a function, but at the same time do this and execute it, dlerror returns a non-null value, an error.
Main.c
#include <dlfcn.h>
#include <stdio.h>
int a = 37;
int main(){
void *lib = dlopen("./libA.so", RTLD_NOW);
if(dlerror() != NULL){
printf("Error loading library.\n");
return -1;
}
void (*f)() = dlsym(lib, "fun");
dlclose(lib);
return 0;
}
Lib.c
#include <stdio.h>
extern int a;
void fun(){
printf("%i\n", a);
}
I tryed to use -fPIC argument in gcc but it doesn't works. De