J T
J T

Reputation: 5136

Shared Library Object File Linkage

I'm interested in solutions for the question below for Linux and Windows, GCC, MinGW, and MSVC (if possible).

I have an application that I've written that supports user-defined shared library imports (add on modules). The application scans a directory, finds *.dll files or *.so files, and loads them dynamically at runtime.

So far, all the user modules have been completely composed of self sufficient code. That is, the object files that make up the DLL/SO yielded no incomplete references from the point of view of the linker.

No I want to allow the modules to be able to use functions that are compiled into object files that make up the binary application that is importing these modules. In other words, I want to allow them to use some of my library code, without having to be compiled into the DLL/SO itself. Unfortunately, in the linker phase when building the DLL/SO, this fails with the complaint that there are unresolved symbols.

Is this possible?

Upvotes: 0

Views: 432

Answers (4)

ninjalj
ninjalj

Reputation: 43688

As for Linux and other ELF platforms, this is perfectly possible. You just need to export the appropriate symbols from your executable, and they will be preferred over the same symbols at the dynamic library. See this question for details.

As suggested by one of the answers to that question, you could instead pass the functions you want to export as callbacks to some initialization function in the dynamic library.

Upvotes: 1

moliad
moliad

Reputation: 1503

why not just make a DLL which is linked by both the main app and all the user libs... this is perfectly legal, safe and does what you want AFAICT.

Upvotes: 1

Ingmar Blonk
Ingmar Blonk

Reputation: 246

Create a library with the code you want to share between the user module and your program. Now the user program and your program can link with this new library.

Upvotes: 2

shelleybutterfly
shelleybutterfly

Reputation: 3247

my first thought is: figure out another way to do this... require the add-ons to communicate via a known interface type, and then there will be no need to try to trick the linker...

Upvotes: 0

Related Questions