Question Details

No question body available.

Tags

java android c linux

Answers (1)

November 29, 2025 Score: 3 Rep: 233,940 Quality: Medium Completeness: 60%

To allow foo.out to be used as both an executable and as a library, you first need to compile it with -FPIE and -pie to allow it to be read via dlopen as well as -rdynamic to export the symbols it contains:

gcc -fPIE -pie -rdynamic foo.c -o foo.out

Then update bar.c to use dlopen and dlsym:

#include 
#include 

int main() { void lib = dlopen("./foo.out",RTLD_LAZY); if (!lib) { printf("dlopen failed: %s\n", dlerror()); return 1; }

int (Main)(int) = dlsym(lib,"Main"); if (!Main) { printf("dlsym failed: %s\n", dlerror()); return 1; }

return Main(2); }

And link with libdl:

gcc bar.c -o bar.out -ldl

Then you'll get the expected results:

[dbush@db-centos7 ~]$ ./bar.out A2B[dbush@db-centos7 ~]$