Qt-created dll does not import into Qt-created app?
-
I'm using:
- QtCreator 10.0.2
- Qt 6.2.9
- cmake-based projects (since that seems to be the default)
- Windows 10
I'm trying to figure out how to create a dll in one project which is then used by another. All projects are created using "Create Project" in the QtCreator.
My dll is a simple LibHello with one class (Hello). At this point it doesn't do anything - it's just the skeleton code that QtCreator put there. It builds to a LibHello.dll.
My application is HelloUser - a Qt widgets based application. The only addition from the skeleton is that I've added a QPushButton and connected it signal to a private slot. The slot just creates and destroys a Hello object from the LibHello library.
My CMakeLists.txt has the following to support using the dll:
INCLUDE_DIRECTORIES(D:/play/hello/LibHello) unset(HELLO CACHE) FIND_LIBRARY(HELLO NAMES "LibHello.dll" PATHS D:/play/hello/build-LibHello-Desktop_x86_windows_msvc2019_pe_64bit-Debug ) target_link_libraries(HelloUser PRIVATE ${HELLO})
But this doesn't work. In fact, it doesn't even complete configuring cmake; it says that it couldn't find the library. Well, the file is definitely there.
If I change FIND_LIBRARY to FIND_FILE, then it configures cmake, but when I try to build, it gives a a "LINK1107: invalid or corrupt file: cannot read at 0x348" and the error indicator references the dll file by path.
If I change the line 'NAMES "LibHello.dll"' to 'NAMES "LibHello.lib"' then it seems to work. But I think the .lib would be for static linking and I'm looking for dynamic linking.
Since I've used all the skeletons from QtCreator, it already has the __declspec(dllexport/dllimport) stuff in place.
Can anyone thing of anything else that might be causing the issue?
Thanks in advance,
-Dan -
When you have built a dynamic library with the Microsoft compiler you get two files:
- Blah.dll, the dynamically loadable runtime library. This ships with the executable.
- Blah.lib, a stub library (an "import library") used by the linker to resolve names to entry points in the dynamic library to allow completion of your application binary. This file is not shipped with the executable.
It is the second file your linker needs.
With MingW the import library will be named blah.dll.a.
You can probably specify the library name without the suffix and CMake will do the right thing.