Esempio libreria eseguibile

From vsd
Jump to navigation Jump to search

File library.c:

// library.c
#include <stdio.h>
#include <stdlib.h>

void foo(int x) {
    printf("foo(%d)\n", x);
}

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "USAGE: %s n\n", argv[0]);
        exit(1);
    }
    foo(atoi(argv[1]));
    return 0;
}

gcc command:

gcc -fPIC -fPIE -pie -rdynamic -o library library.c

library is an executable:

$ ./library 44
foo(44)

but it can be loaded as a shared library.

This is the file dltest.c:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

void (*pfoo)(int x);
int (*pmain)(int argc, char **argv);

int main(int argc, char *argv[])
{
        void *handle;
        double (*cosine)(double);
        char *error;

        handle = dlopen("./library", RTLD_LAZY);
        if (!handle) {
                fprintf(stderr, "%s\n", dlerror());
                exit(EXIT_FAILURE);
        }

        dlerror();    /* Clear any existing error */
        printf("dlopen done\n");
        pfoo = (typeof(pfoo)) dlsym(handle, "foo");
        printf("pfoo %p\n",pfoo);
        pmain = (typeof(pmain)) dlsym(handle, "main");
        printf("pmain %p\n",pmain);

        pfoo(42);

        pmain(argc, argv);

        error = dlerror();
        if (error != NULL) {
                fprintf(stderr, "%s\n", error);
                exit(EXIT_FAILURE);
        }

        dlclose(handle);
        exit(EXIT_SUCCESS);
}

Let us compile dltest...

gcc -o dltest dltest.c -ldl

and launch it:

$ ./dltest 44
dlopen done
pfoo 0x7f04c6da7165
pmain 0x7f04c6da7189
foo(42)
foo(44)

It works!


Credits:

an alternative command to compile library.c is

 gcc -fPIC -pie -o library library.c -Wl,-E