Hoofamon Hoofamon - 2 months ago 9
C++ Question

With g++ Is there way for a dynamically linked library to be loaded under a specified namespace without modifying source?

With g++ is there a way to link a library so that all symbols are under a new namespace?

For example, I'd like to use a library in my program which has conflicting symbols with my existing code. Is there a way to encapsulate the linked library under a new name space through the linker, without the need to modify source code?

Edit

Here is some more detail:

I am working on a Qt application, which functions as an "Application Manager" for an embedded device. This application links the Qt module "webkitwidgets", which is a library.

the applications managed by this application manager are built as shared libraries, and when they are launched by this manager program a wrapper programed (Launcher) is forked, the launcher uses QLibrary to load the shared library:

bool ProcessInitImpl::loadLibrary(const QString& libraryPath) {
qTrace() << libraryPath;

QLibrary lib(libraryPath);
lib.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
if (lib.load()) {
qDebug() << libraryPath << " is loaded successfully!";
typedef void (*RegistrationMethod)();
RegistrationMethod registrationMethodInTheSharedLibrary = (RegistrationMethod) lib.resolve("registerMetaTypes");
if (registrationMethodInTheSharedLibrary) {
registrationMethodInTheSharedLibrary();
return true;
}
}
qCritical() << libraryPath << " failed to load!" << lib.errorString();
return false;
}


the problem is that this launcher is suppose to be decoupled from the manager, using IPC to communicate between the launcher instances and the manager. However, the launcher is linked to the manager, which in turn is linked to webkitwidgets. There is an application which contains conflicting symbols within webkitwidgets.

Given all of that above, the only thing that came to mind for me was modifying the launcher wrapper so that it had no dependencies on the manager and thus didn't link webkitwidgets. I just wanted to see if there was another way to address this before going down that path.

Answer

With g++ is there a way to link a library so that all symbols are under a new namespace?

Probably not (namespaces are a C++ thing and are implemented by name mangling), and you don't specify what exactly loading a dynamically linked library means to you and you don't tell anything about the actual conflicts you've got. In reality, it is quite complex. Read Levine's Linkers and Loaders and Drepper's How to Write Shared Libraries for details, and also the documentation of binutils ld, ld-linux(8) etc. Read also about dlmopen(3) (which I never used).

For example, I'd like to use a library in my program which has conflicting symbols with my existing code.

Don't touch the library, or the way you are loading it, but touch your program. It would be much simpler (and you could use tools for that, perhaps code your own GCC plugin or use GCC MELT for that). Perhaps it could be as simple as adding a few namespace and or using in your own C++ source code. YMMV.

Practically speaking, how to approach the problem depends a lot on the actual conflicts you are observing and how much you have ...

Comments