title: Why can CLion correctly build and link Qt, but not run my executable?
body: <h1>Summary</h1>
  <p>I am trying to compile ánd run/debug Qt code in Clion on Windows. Both CMake and the building process return no errors.</p>
  <p>The following scenarios occur:</p>
  <ol>
  <li><p>When I try to run I get <code>Process finished with exit code -1073741511 (0xC0000139)</code></p>
  </li>
  <li><p>When I try to debug I get <code>Process finished with exit code 1</code></p>
  </li>
  <li><p>When I try to run the executable via Windows CMD directly, the executable runs as intended.</p>
  </li>
  <li><p>When I copy all dll files from <code>../Qt/5.12.6/mingw73_64/bin</code> to the project's <code>cmake-build-debug</code> folder, the executable runs and debugs within CLion as expected.</p>
  </li>
  </ol>
  <h1>My setup</h1>
  <ul>
  <li>Windows 10</li>
  <li>Qt 5.12.6 (mingw73_64 build)</li>
  <li>CLion 2019.2.5</li>
  <li>MinGW (x86_64-8.1.0-win32-seh-rt_v6_rev0)</li>
  <li>CMake (bundled, 3.15.3)</li>
  </ul>
  <h1>Other findings</h1>
  <p>I believe there are many related topics on StackOverflow that deal with the same issue. But none manage to provide a definitive answer to what I believe to be a Path/Environment issue. Many suggestions boil down to &quot;Add Qt to your path/PATH/Path environment variable and reboot reboot reboot!&quot;, and/or mostly pertain to linux installs. Hence I hope this becomes a more complete question and answer for people running into the same error code within this context, as it is likely related to this same issue.</p>
  <p>As things work outside of CLion (as shown by (3)) and work inside of CLion when I copy DLLs (4), I believe I am dealing a dynamic linking issue as a result of CLion related environment issues. Adding the Qt bin folder, which is <code>C:\Qt\5.12.6\mingw73_64\bin</code>, to my <code>System Environment Variables</code> made it so I could run the exe file directly from CMD. Note that I added the Qt bin folder path to the <code>Path</code> variable.</p>
  <p>Given that some mentioned online that it is possibly an issue with the user variables due to CLion running as a certain system user, I also added said path as a <code>User Environment Variable</code>, again <code>Path</code>. But alas.</p>
  <p>Additionally, I tried adding it as an environment variable directly in CLion via <code>Settings -&gt; Appearance &amp; Behavior -&gt; Path Variables</code>. Here I tried mapping the Qt bin folder to <code>Path</code>, <code>PATH</code>, and <code>QT_DIR</code> respectively. Still no success, even though I tried many reboots. Both Windows restarts and real shutdowns were attempted many times in between changing paths etc.</p>
  <h1>Main question:</h1>
  <p>How can I resolve the issue I described, so I can run and debug my Qt builds in CLion without having to copy Qt related DLLs to my <code>cmake-build-debug</code> where the executable is located.</p>
  <h1>CMakeLists.txt</h1>
  <p>Within <code>Settings -&gt; Build, Execution, Deployment -&gt; CMake</code> I have set <code>CMake options: </code> to <code>-DCMAKE_PREFIX_PATH=C:\\Qt\\5.12.6\\mingw73_64\\lib\\cmake</code></p>
  <pre><code>cmake_minimum_required(VERSION 3.8)
  project(HelloWorld)
  
  set(CMAKE_CXX_STANDARD 14)
  
  # Include a library search using find_package()
  # via REQUIRED, specify that libraries are required
  find_package(Qt5Core REQUIRED)
  find_package(Qt5Gui REQUIRED)
  find_package(Qt5Widgets REQUIRED)
  
  set(SOURCE_FILES main.cpp)
  add_executable(${PROJECT_NAME} ${SOURCE_FILES})
  
  # specify which libraries to connect
  target_link_libraries(${PROJECT_NAME} Qt5::Core)
  target_link_libraries(${PROJECT_NAME} Qt5::Gui)
  target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
  </code></pre>
  <h1>main.cpp</h1>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;QtWidgets/QApplication&gt;
  #include &lt;QtWidgets/QWidget&gt;
  #include &lt;QtWidgets/QGridLayout&gt;
  #include &lt;QtWidgets/QLabel&gt;
  
  int main (int argc, char * argv []) {
  
      QApplication app (argc, argv);
  
      QWidget widget;
      widget.resize (640, 480);
      widget.setWindowTitle (&quot;Hello, world !!!&quot;);
  
      QGridLayout * gridLayout = new QGridLayout (&amp; widget);
  
      QLabel * label = new QLabel (&quot;Hello, world !!!&quot;);
      label-&gt; setAlignment (Qt :: AlignVCenter | Qt :: AlignHCenter);
      gridLayout-&gt; addWidget (label);
  
      widget.show ();
  
      return app.exec ();
  }
  </code></pre>