Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives "undefined reference" error
<p>When I run <code>make</code> to compile the Makefile produced by <code>cmake</code>, to compile an hello world example of a Qt5 application, the compilation fails with the following error:</p>

<p><a href="https://i.stack.imgur.com/xtOP5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xtOP5.png" alt="enter image description here"></a></p>

<p><strong>Why is the compilation failing?</strong></p>

<p>(details of what exactly I'm trying to do follow)</p>

<hr>

<p>I'm under Windows 10, using the Qt5.5 binaries downloaded from <a href="https://www.qt.io/download-open-source/" rel="nofollow noreferrer">Qt official website</a>, mingw-w64 <code>gcc</code> and <code>g++</code> shipped with <a href="http://win-builds.org/doku.php" rel="nofollow noreferrer">WinBuilds</a>, and <code>cmake</code> v3.6 downloaded from the <a href="https://cmake.org/download/" rel="nofollow noreferrer">official website</a> and installed with the <code>Windows win64-x64 Installer</code>.</p>

<p>I'm trying to compile the following hello world test file, provided in <a href="https://wiki.qt.io/Qt_for_Beginners" rel="nofollow noreferrer">Qt5's official wiki</a>:</p>

<pre><code>#include &lt;QCoreApplication&gt;

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

    return app.exec();
}
</code></pre>

<p>The Makefile is built successfully using the command <code>cmake -G "MinGW Makefiles" ..</code> from a folder called <code>build</code> inside the directory containing the .cpp file. The following CMakeLists.txt file (taken from <a href="http://doc.qt.io/qt-5/cmake-manual.html" rel="nofollow noreferrer">Qt5's cmake wiki page</a>, with the addition of the specification of the CMAKE_PREFIX_PATH variable, which is required as for example discussed in this <a href="https://stackoverflow.com/a/28327499/4063051">SO post</a>) was used:</p>

<pre><code>cmake_minimum_required(VERSION 2.8.11)

SET(CMAKE_C_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-gcc-4.8.3.exe)
SET(CMAKE_CXX_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-g++-4.8.3.exe)

project(testproject)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_PREFIX_PATH "C:/WinBuilds/lib64/cmake")
set(CMAKE_PREFIX_PATH "C:/Qt/5.5/mingw492_32/lib/cmake")

find_package(Qt5Widgets)

add_executable(testfile WIN32 test.cpp)

target_link_libraries(testfile Qt5::Widgets)
</code></pre>

<p>(I did not use the cmake shipped with Qt5 as that did not work).</p>

<p>Now, the problem arises when I run <code>make</code> (or more precisely, <code>mingw32-make</code>, again shipped with WinBuilds) on the Makefile produced by cmake.
When I do this, the compilation fails with the following error (same one showed in the screenshot above):</p>

<pre><code>CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x35): undefined reference to `__imp__ZN16QCoreApplicationC1ERiPPci'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x3e): undefined reference to `__imp__ZN16QCoreApplication4execEv'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x50): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x67): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\testfile.dir/objects.a(test.cpp.obj): bad reloc address 0xc in section `.xdata'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\testfile.dir\build.make:127: recipe for target 'testfile.exe' failed
mingw32-make[2]: *** [testfile.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/testfile.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/testfile.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
</code></pre>

<p><strong>Why is the compilation failing?</strong></p>

<p>A similar <em>undefined reference</em> error was reported in this other <a href="https://stackoverflow.com/q/18260607/4063051">SO question</a>, but the reason there seemed to be different than the present case.</p>