title: CMake project using Qt libraries in Visual Studio 2017: can't find Qt dll when running the exe
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>Below are my questions:</p>
  
  <ul>
  <li>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?</li>
  <li>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!</li>
  <li>why does the exe try to find the "Qt5Sql.dll" but not "QtCore.dll"? I believe both are used in the test files.</li>
  <li>Why does it look for dll files when I already linked the lib files in the CMakeLists.txt.</li>
  </ul>
  
  <p>Tools and system used: CMake 3.8.1, Qt 5.9 beta, Visual Studio 2017, windows 10</p>
  
  <p>Below is the CMakeLists.txt I used for the test:</p>
  
  <pre><code>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
      )
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>#include &lt;QtDebug&gt;
  #include &lt;QString&gt;
  #include &lt;QtSql&gt;
  #include &lt;QSqlDatabase&gt;
  #include &lt;QtSql/QSqlDatabase&gt;
  #include &lt;QtSql/QSqlError&gt;
  #include &lt;QtSql/QSqlDriver&gt;
  
  
  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() &lt;&lt; db.connectionName();
  
      return 0;
  }
  </code></pre>
  
  <p>[EDIT] Below is the log file from Dependency Walker </p>
  
  <pre><code>***************************| 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
  </code></pre>