1
votes

When I run make to compile the Makefile produced by cmake, to compile an hello world example of a Qt5 application, the compilation fails with the following error:

enter image description here

Why is the compilation failing?

(details of what exactly I'm trying to do follow)


I'm under Windows 10, using the Qt5.5 binaries downloaded from Qt official website, mingw-w64 gcc and g++ shipped with WinBuilds, and cmake v3.6 downloaded from the official website and installed with the Windows win64-x64 Installer.

I'm trying to compile the following hello world test file, provided in Qt5's official wiki:

#include <QCoreApplication>

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

    return app.exec();
}

The Makefile is built successfully using the command cmake -G "MinGW Makefiles" .. from a folder called build inside the directory containing the .cpp file. The following CMakeLists.txt file (taken from Qt5's cmake wiki page, with the addition of the specification of the CMAKE_PREFIX_PATH variable, which is required as for example discussed in this SO post) was used:

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)

(I did not use the cmake shipped with Qt5 as that did not work).

Now, the problem arises when I run make (or more precisely, mingw32-make, 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):

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

Why is the compilation failing?

A similar undefined reference error was reported in this other SO question, but the reason there seemed to be different than the present case.

1
Are you sure that the official Qt-5.5 download had a 64 bit mingw compiled binary? I thought they only provided 32 bit binaries for mingw.drescherjm
@drescherjm thank you, I did not think of the possibility of having a 32 bit version of Qt5. That may very well be the problem. Under the folder tree of Qt there are mingw492_32 folders, which may mean that I have a 32 bit distribution of Qt indeed (though the gcc and g++ under this folder do not seem to work at all). Do you know of any easy way to check the architecture for which Qt was compiled?glS
I would build Qt itself with the compiler you want to use for your applications. Building Qt will take 1 hour or so however. It is not that difficult to configure. You will need python, perl and ruby to build. I use ActiveState versions of perl and python and ruby from rubyinstaller.drescherjm
@drescherjm well that is annoying... but looking through the relevant pages in Qt's wiki (on MinGW, MinGW-64-bit) I'm more and more convinced that this is exactly the problem I'm facing. On the second linked wiki page there is a link to download a binary for Qt5.6 compiled with mingw64, I'm downloading it now and will report on the results. I also tried to compile with 32 bit MinGW. Doing so resulted in no compilation errors, but an error opening the resulting executable file. This is probably material for another question though.glS
@drescherjm I confirm that that was indeed the problem. I downloaded the binary for Qt5.6 compiled with mingw64 provided in the wiki and it now works (though the simple .cpp code in the question does not do anything, following examples in the Qt for beginners guide work as expected)! Thanks a lot! If you make it an answer I'll accept itglS

1 Answers

2
votes

The problem is you are using 32 bit mingw compiled Qt binaries when you are trying to build a 64 bit Qt application. You need to use 64 bit Qt binaries compiled with mingw for this to work.