J. Chau
J. Chau

Reputation: 11

Is it possible to prevent function override in a C shared library?

I am developing a shared library on Linux in C using GCC. I noticed that any function in the shared library can be overridden by redefining it in the main application. Is there a way to prevent specific functions from being overridden in a shared library?

Upvotes: 0

Views: 136

Answers (1)

user14215102
user14215102

Reputation:

If you don't need to export that function to the world, make it static

static void a() {
}
void b() {
    a();
}

the main application will have no way to directly call a, but maybe you don't need that.

If you want to also export that function the the world, you could do:

static void real_a() {
   /* the actual implementation */
}
void a() { /* wrapper for export */
    real_a();
}
void b() { /* your library code has to call the real function */
    ...
    real_a();
}

The main application can still override a, but that does not influence the library code, which is calling real_a internally.

Upvotes: 1

Related Questions