Delay loading DLL in QT

Is it possible to delay loading a DLL in QT? For example, my program has a dependency on some third-party DLLs, and I want to remove it.

What should I write in a .pro file to remove dependencies?

Is it possible to store DLLs in resources?

Is it possible to load DLLs “globally”? Therefore, some functions from the DLL (e.g. func1) will remain func1 in my code.

+3
source share
2 answers

Yes, you want to use a class QLibrary. It is specifically designed to load shared libraries at runtime.

.pro. , DLL . , (PATH Windows, LD_LIBRARY_PATH Linux, DYLD_LIBRARY_PATH Mac), .

, , , "func1()" .

[EDIT]

dll , . IMO, , Windows 7. - , , ..

foo.cpp,

#include <QtCore/qglobal.h>

extern "C" Q_DECL_EXPORT int foo(int value) {
  return value + 42;
}

bar.pro, foo library

SOURCES += main.cpp
RESOURCES += resources.qrc

main.cpp

#include <QtCore>
#include <iostream>

int main(int argc, char **argv) {
  QCoreApplication app(argc, argv);

  // Copy resource dll to temporary file.
  QFile::copy(":/lib/Foo.dll", QDir::temp().filePath("Foo.dll"));

  // Load the temporary file as a shared library.
  QLibrary foo_lib(QDir::temp().filePath("Foo.dll"));
  typedef int (*FooDelegate)(int);
  FooDelegate foo = (FooDelegate)foo_lib.resolve("foo");

  if (foo) {
    std::cout << "foo(13) = " << foo(13) << std::endl;
  }
}
+5

Microsoft... , QT , , / .

, , DLL /delayload:[dllname] delayimp.lib. .cpp

#pragma comment(lib, "delayimp")

.

, .pro, delayload .

. Microsoft.

+3

All Articles