C++ / C function to load a dynamic library on demand from a C++ function

I have dynamic library created as follows

cat myfile.cc

struct Tcl_Interp;
extern "C" int My_Init(Tcl_Interp *) { return 0; }

1) complile the cc file
g++ -fPIC -c myfile.cc

2) Creating a shared library

g++ -static-libstdc++ -static-libgcc -shared -o libmy.so myfile.o -L/tools/linux64/qt-4.6.0/lib -lQtCore -lQtGui


3) load the library from a TCL proc
then I give command

tclsh
and given command
% load libmy.so

is there any C++ function equivalent to load that can load the shared library on demand from another C++ function.

There are OS-specific (probably C) functions, but you seem to link in Qt, so you presumably do use Qt and therefore:
http://doc.qt.io/qt-4.8/qlibrary.html#details
One more question in this case I do not need to link to my final executable and load this .so when inside from the C++ functions on runtime correct ?
Last edited on
Correct.

There is static linking, where the object code from (object files and) static libraries is added directly to the executable on compilation.

There is implicit dynamic linking, where linker adds mere "stubs" to the executable, which forces the dynamic linker to load necessary shared objects (dynamic libraries, .so, .dyld, .dll) when the executable loads.

There is explicit dynamic linking, where nothing about the shared object(s) is linked/stored in the executable (except the string literals in executable code):
1
2
3
4
5
QLibrary myLib("my");
typedef int (*MyPrototype)(Tcl_Interp *);
MyPrototype myFunction = (MyPrototype) myLib.resolve("My_Init");
if (myFunction)
    ...


This is what most "plugin"-implementations do use; there might exist no plugins at the time the executable is compiled.


An example of explicit linking: an older graphical application had several options for rendering -- if an OpenGL implementation (i.e. library file) was found, it was used. Otherwise a lesser built-in software renderer was used. One cannot do such thing with implicit linking, because in absence of "optional" library the dynamic linker fails and the app crashes on load.
This is not clear . When I trying the link the application my linker fails as gives me errors like

Model]+0xe0): undefined reference to `QAbstractItemModel::supportedDropActions() const'

as I have not mentioned -lQTCore or -lQtGui in final linking of the executable . If you can guide me how it is possible in my case that I do not link the libraries libQtGui.so and libQtCore.so in my executable and then load the same at run time inside a certain function

I would not try to explicitly link libQt*. If your application has any QObjects (and QApplication*), then you most likely have to have them implicitly linked.
Is there any way I defer the loading of libQt libraries after a certain function call

i.e I link the QT libraries but defer its loading after a certain function call
I have no idea how convoluted the Qt libraries are.

Why do you include the Qt libs on your link command?


If you are operating only in Linux, then you can use the system's functions directly. The Qt's QLibrary is just a C++ proxy to them. (In Windows it uses totally different Windows system functions.)

See:
man dlopen
Topic archived. No new replies allowed.