Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake project using Qt libraries in Visual Studio 2017: can't find Qt dll when running the exe

I am trying to use Qt libraries for a CMake project with Visual Studio. I was able to find all Qt5 components (such as Qt5Sql and Qt5Core) using CMake. I also had access to the Qt objects when I did the source files for the testing.

However after I finish the build and try to run the resulting executable, it complains that "Qt5Sql.dll is missing from your computer". I then copied the dll to the build folder where the executable resides, but that did not solve the problem. However, while I think Qt5Core is also used in the test, it does not complain about Qt5Core and did not say it's missing.

Below are my questions:

  • Why the Qt components are available for source files, but unavailable for the executable? Where should I save the dll files so that the exe files can find them?
  • I have read quite some documentation on static vs dynamic vs shared libraries but am still confused how they are applied in this type of situation. How can I include the Qt components in the deployment so that the exe don't need to find the dlls? This is a very basic question but any help is appreciated!
  • why does the exe try to find the "Qt5Sql.dll" but not "QtCore.dll"? I believe both are used in the test files.
  • Why does it look for dll files when I already linked the lib files in the CMakeLists.txt.

Tools and system used: CMake 3.8.1, Qt 5.9 beta, Visual Studio 2017, windows 10

Below is the CMakeLists.txt I used for the test:

cmake_minimum_required(VERSION 3.6)
project(testQt)

set(CMAKE_PREFIX_PATH D:\\Qt\\5.9\\msvc2015_64\\lib\\cmake)
set (CMAKE_AUTOMOC ON)
set (CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5 REQUIRED COMPONENTS
    Core
    Sql
    Widgets
    Gui
    )

 add_executable(test test.cpp)
 target_link_libraries(test
    Qt5::Core 
    Qt5::Sql
    )

Below is the test.cpp. I have tried the test.cpp in Qt creater and it works there. It simply establish connnection to a local database.

#include <QtDebug>
#include <QString>
#include <QtSql>
#include <QSqlDatabase>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>


int main()
{

    QString servername = "DESKTOP-NAME\\SQLEXPRESS";
    QString dbname = "sql_db_test";
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");  
    db.setConnectOptions();
    QString dsn = QString("Driver={SQL Server};Server=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
    db.setDatabaseName(dsn);
    qDebug() << db.connectionName();

    return 0;
}

[EDIT] Below is the log file from Dependency Walker

***************************| Module Dependency Tree |***************************
*                                                                              *
* Legend: F  Forwarded Module   ?  Missing Module        6  64-bit Module      *
*         D  Delay Load Module  !  Invalid Module                              *
*         *  Dynamic Module     E  Import/Export Mismatch or Load Failure      *
*                               ^  Duplicate Module                            *
*                                                                              *
********************************************************************************

[  6] test.EXE
     [ ? ] QT5SQLD.DLL
     [ ? ] QT5CORED.DLL
     [  6] MSVCP140D.DLL
          [ ^6] VCRUNTIME140D.DLL
          [ ^6] UCRTBASED.DLL
          [ ^6] KERNEL32.DLL
               [F^6] NTDLL.DLL
          [D 6] CONCRT140D.DLL
               [ ^6] MSVCP140D.DLL
               [ ^6] VCRUNTIME140D.DLL
               [ ^6] UCRTBASED.DLL
               [ ^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
     [  6] VCRUNTIME140D.DLL
          [ ^6] UCRTBASED.DLL
          [ ^6] KERNEL32.DLL
               [F^6] NTDLL.DLL
     [  6] UCRTBASED.DLL
          [  6] API-MS-WIN-CORE-STRING-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-ERRORHANDLING-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-FILE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-TIMEZONE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-NAMEDPIPE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-FILE-L2-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-HEAP-L1-1-0.DLL
               [F^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
          [  6] API-MS-WIN-CORE-SYSINFO-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-LIBRARYLOADER-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-SYNCH-L1-1-0.DLL
               [F^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
          [  6] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-0.DLL
               [F^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
          [  6] API-MS-WIN-CORE-PROCESSENVIRONMENT-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-DATETIME-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-LOCALIZATION-L1-2-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-SYNCH-L1-2-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-CONSOLE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-DEBUG-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-RTLSUPPORT-L1-1-0.DLL
               [F^6] NTDLL.DLL
          [  6] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-1.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-FILE-L1-2-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-PROFILE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-MEMORY-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [  6] API-MS-WIN-CORE-UTIL-L1-1-0.DLL
               [F^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
          [  6] API-MS-WIN-CORE-INTERLOCKED-L1-1-0.DLL
               [F^6] KERNEL32.DLL
                    [F^6] NTDLL.DLL
     [  6] KERNEL32.DLL
          [ ? ] API-MS-WIN-CORE-RTLSUPPORT-L1-2-0.DLL
          [  6] NTDLL.DLL
          [  6] KERNELBASE.DLL
               [ ^6] NTDLL.DLL
               [ ^6] API-MS-WIN-EVENTING-PROVIDER-L1-1-0.DLL
                    [F^6] ADVAPI32.DLL
                         [F^6] NTDLL.DLL
               [ ? ] API-MS-WIN-CORE-APIQUERY-L1-1-0.DLL
               [D? ] EXT-MS-WIN-ADVAPI32-REGISTRY-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-APPCOMPAT-L1-1-0.DLL
               [D? ] EXT-MS-WIN-NTUSER-STRING-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-FILE-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-DATETIME-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-QUIRKS-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-QUIRKS-L1-1-1.DLL
               [D? ] EXT-MS-WIN-KERNEL32-SIDEBYSIDE-L1-1-0.DLL
               [D? ] EXT-MS-WIN-MRMCORER-RESMANAGER-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-WINDOWSERRORREPORTING-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-WINDOWSERRORREPORTING-L1-1-1.DLL
               [D? ] EXT-MS-WIN-GPAPI-GROUPPOLICY-L1-1-0.DLL
               [D? ] EXT-MS-WIN-NTDSAPI-ACTIVEDIRECTORYCLIENT-L1-1-0.DLL
               [D? ] EXT-MS-WIN-NTDSAPI-ACTIVEDIRECTORYCLIENT-L1-1-1.DLL
               [D? ] EXT-MS-WIN-SHELL32-SHELLCOM-L1-1-0.DLL
               [D? ] EXT-MS-WIN-ADVAPI32-NTMARTA-L1-1-0.DLL
               [D? ] EXT-MS-WIN-SECURITY-CAPAUTHZ-L1-1-0.DLL
               [D? ] EXT-MS-WIN-FECLIENT-ENCRYPTEDFILE-L1-1-0.DLL
               [D? ] EXT-MS-WIN-SECURITY-EFSWRT-L1-1-1.DLL
               [D? ] EXT-MS-WIN-APPMODEL-DAXCORE-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-ERRORHANDLING-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNEL32-REGISTRY-L1-1-0.DLL
               [D? ] EXT-MS-WIN-KERNELBASE-PROCESSTHREAD-L1-1-0.DLL
               [D? ] EXT-MS-WIN-ADVAPI32-NPUSERNAME-L1-1-0.DLL
               [D? ] EXT-MS-WIN-APPXDEPLOYMENTCLIENT-APPXDEPLOY-L1-1-0.DLL
               [D? ] EXT-MS-WIN-APPXDEPLOYMENTCLIENT-APPXDEPLOYONECORE-L1-1-0.DLL
               [D? ] EXT-MS-WIN-COM-PSMREGISTER-L1-2-1.DLL
          [ ? ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-3.DLL
          [ ? ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-2.DLL
          [ ? ] API-MS-WIN-CORE-REGISTRY-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-HEAP-L1-2-0.DLL
          [ ? ] API-MS-WIN-CORE-HEAP-L2-1-0.DLL
          [ ? ] API-MS-WIN-CORE-MEMORY-L1-1-2.DLL
          [ ? ] API-MS-WIN-CORE-ENCLAVE-L1-1-0.DLL
          [ ^6] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [ E6] API-MS-WIN-CORE-SYNCH-L1-2-0.DLL
               [F^6] KERNEL32.DLL
                    [FE6] API-MS-WIN-CORE-SYNCH-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-SYNCH-L1-2-1.DLL
          [ ? ] API-MS-WIN-CORE-FILE-L1-2-1.DLL
          [ ? ] API-MS-WIN-CORE-FILE-L1-2-2.DLL
          [ ? ] API-MS-WIN-CORE-DELAYLOAD-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-IO-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-JOB-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-THREADPOOL-LEGACY-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-THREADPOOL-PRIVATE-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-LIBRARYLOADER-L1-2-2.DLL
          [ ? ] API-MS-WIN-CORE-LIBRARYLOADER-L1-2-0.DLL
          [ ? ] API-MS-WIN-CORE-LIBRARYLOADER-L2-1-0.DLL
          [ ? ] API-MS-WIN-CORE-NAMEDPIPE-L1-2-2.DLL
          [ ? ] API-MS-WIN-CORE-NAMEDPIPE-L1-2-0.DLL
          [ ? ] API-MS-WIN-CORE-DATETIME-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-DATETIME-L1-1-2.DLL
          [ ? ] API-MS-WIN-CORE-SYSINFO-L1-2-1.DLL
          [ ? ] API-MS-WIN-CORE-SYSINFO-L1-2-3.DLL
          [ ^6] API-MS-WIN-CORE-TIMEZONE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [ ? ] API-MS-WIN-CORE-LOCALIZATION-L1-2-1.DLL
          [ ? ] API-MS-WIN-CORE-PROCESSSNAPSHOT-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-PROCESSENVIRONMENT-L1-2-0.DLL
          [ ^6] API-MS-WIN-CORE-STRING-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [ ? ] API-MS-WIN-CORE-DEBUG-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-3.DLL
          [ ? ] API-MS-WIN-CORE-FIBERS-L1-1-1.DLL
          [ ^6] API-MS-WIN-CORE-PROFILE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [ ? ] API-MS-WIN-SECURITY-BASE-L1-2-0.DLL
          [ ? ] API-MS-WIN-SECURITY-APPCONTAINER-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-COMM-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-REALTIME-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-WOW64-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-WOW64-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-PROCESSTOPOLOGY-L1-2-0.DLL
          [ ? ] API-MS-WIN-CORE-NAMESPACE-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-FILE-L2-1-2.DLL
          [ ? ] API-MS-WIN-CORE-FILE-L2-1-1.DLL
          [ ? ] API-MS-WIN-CORE-XSTATE-L2-1-0.DLL
          [ ? ] API-MS-WIN-CORE-LOCALIZATION-L2-1-0.DLL
          [ ? ] API-MS-WIN-CORE-NORMALIZATION-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-FIBERS-L2-1-1.DLL
          [ ? ] API-MS-WIN-CORE-LOCALIZATION-PRIVATE-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-SIDEBYSIDE-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-APPCOMPAT-L1-1-1.DLL
          [ ? ] API-MS-WIN-CORE-WINDOWSERRORREPORTING-L1-1-0.DLL
          [ ^6] API-MS-WIN-CORE-CONSOLE-L1-1-0.DLL
               [F^6] KERNEL32.DLL
          [ ? ] API-MS-WIN-CORE-CONSOLE-L2-1-0.DLL
          [ ? ] API-MS-WIN-CORE-PSAPI-L1-1-0.DLL
          [ ? ] API-MS-WIN-CORE-PSAPI-ANSI-L1-1-0.DLL
          [  6] API-MS-WIN-EVENTING-PROVIDER-L1-1-0.DLL
               [F 6] ADVAPI32.DLL
                    [  6] MSVCRT.DLL
                         [ ^6] NTDLL.DLL
                         [ ^6] API-MS-WIN-CORE-CONSOLE-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ? ] API-MS-WIN-CORE-DATETIME-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-DEBUG-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-FIBERS-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-FILE-L1-2-1.DLL
                         [ ^6] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ? ] API-MS-WIN-CORE-HEAP-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-LOCALIZATION-L1-2-1.DLL
                         [ ? ] API-MS-WIN-CORE-LIBRARYLOADER-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-MEMORY-L1-1-2.DLL
                         [ ? ] API-MS-WIN-CORE-NAMEDPIPE-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-PROCESSENVIRONMENT-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-2.DLL
                         [ ^6] API-MS-WIN-CORE-PROFILE-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ^6] API-MS-WIN-CORE-STRING-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ E6] API-MS-WIN-CORE-SYNCH-L1-2-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ? ] API-MS-WIN-CORE-SYSINFO-L1-2-1.DLL
                         [ ^6] API-MS-WIN-CORE-UTIL-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                                   [F^6] NTDLL.DLL
                         [ ^6] KERNELBASE.DLL
                         [F^6] NTDLL.DLL
                    [ ^6] NTDLL.DLL
                    [ E6] API-MS-WIN-EVENTING-CONTROLLER-L1-1-0.DLL
                         [F^6] ADVAPI32.DLL
                    [ ? ] API-MS-WIN-EVENTING-CONSUMER-L1-1-0.DLL
                    [ ^6] KERNELBASE.DLL
                    [  6] SECHOST.DLL
                         [ ^6] NTDLL.DLL
                         [ ? ] API-MS-WIN-CORE-LIBRARYLOADER-L1-2-0.DLL
                         [ ^6] RPCRT4.DLL
                         [ ? ] API-MS-WIN-CORE-CRT-L1-1-0.DLL
                         [ ? ] API-MS-WIN-CORE-THREADPOOL-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-FILE-L1-2-1.DLL
                         [ ? ] API-MS-WIN-CORE-IO-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-DEBUG-L1-1-1.DLL
                         [ ? ] API-MS-WIN-CORE-ERRORHANDLING-L1-1-1.DLL
                         [ ^6] API-MS-WIN-CORE-HANDLE-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ? ] API-MS-WIN-CORE-HEAP-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-REGISTRY-L1-1-0.DLL
                         [ ? ] API-MS-WIN-CORE-HEAP-OBSOLETE-L1-1-0.DLL
                         [ ? ] API-MS-WIN-CORE-MEMORY-L1-1-2.DLL
                         [ ? ] API-MS-WIN-CORE-PROCESSTHREADS-L1-1-2.DLL
                         [ ^6] API-MS-WIN-CORE-STRING-L1-1-0.DLL
                              [F^6] KERNEL32.DLL
                         [ E6] API-MS-WIN-CORE-SYNCH-L1-2-0.DLL
                              [F^6] KERNEL32.DLL
                         [ ? ] API-MS-WIN-CORE-SYSINFO-L1-2-1.DLL
                         [ ? ] API-MS-WIN-CORE-WOW64-L1-1-0.DLL
                         [ ? ] API-MS-WIN-SECURITY-BASE-L1-2-0.DLL
                         [ ? ] API-MS-WIN-CORE-APIQUERY-L1-1-0.DLL
                         [ ? ] API-MS-WIN-CORE-DELAYLOAD-L1-1-1.DLL
                         [D? ] EXT-MS-WIN-EVENTING-RUNDOWN-L1-1-0.DLL
                         [D? ] EXT-MS-WIN-SECURITY-CHAMBERS-L1-1-0.DLL
                         [D^6] CRYPTBASE.DLL
like image 597
DavidY Avatar asked May 28 '17 15:05

DavidY


1 Answers

When Qt is built as DLL the files in D:\Qt\5.9\msvc2015_64\lib\cmake only find the import libraries. Together with the include files those are sufficient for the compiler and the link process to resolve open dependencies. You have to add the binary directories of Qt D:\Qt\5.9\msvc2015_64\bin and D:\Qt\5.9\msvc2015_64\plugins\platforms to the environment by appending it to the PATH variable.

PATH=D:\Qt\5.9\msvc2015_64\bin;D:\Qt\5.9\msvc2015_64\plugins\platforms;%PATH%

For testing in Visual Studio you could add this setting under the Configuration Properties Debugging section in Environment. Go to Solution Explorer right click on the project ("test" in your sample case) and select Properties to show the Configuration Properties. See Working with Project Properties

If you later build an installer for your project with CMake/CPack the Qt Windows deployment tool comes handy for identifying the Qt DLLs needed for distribution. See Qt Deployment

like image 175
vre Avatar answered Oct 12 '22 23:10

vre