Windows中使用QT-qmake自动生成的Makefile的minGW(g ++)链接错误

时间:2019-02-01 18:54:42

标签: c++ qt g++ mingw qmake

我正在尝试使用MinGW编译器构建一个简单的QT程序。我最终希望将我的Linux开发的QT / C ++程序移植到Windows中。我从他们的站点下载了QT_4.3.5(这是我的程序使用的)和MinGW。我相信这是一个32位编译器。我正在使用“ cmd”来构建程序,类似于在Linux中构建程序的方式。我的程序如下所示。

#include <QApplication>
#include <QPushButton>
#include <cstdio>

int main(int argc, char *argv[])   //This should work with QT4?
{
printf("Hello\n");
QApplication app(argc,argv);
QPushButton hello ("Hello world!");
hello.resize(100,30);
hello.show();
return app.exec();
} 

我能够在Linux 5.7中成功构建并运行该程序。因此,在Windows中,我创建了一个“ main.cpp”文件,并使用qmake来创建.pro文件和Makefile。我正在使用QT4.3.5中的qmake。我使用了“ qmake -project”和qmake。它在Windows中成功构建了.pro文件和Makefile。但是当我运行make时,它给了我一个“无法识别的命令行选项'-Wl'”

我相信qmake版本和minGW可能存在兼容性问题。 Makefile中的相关行是

LFLAGS        =        -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows

然后我尝试删除-Wl并给出错误,提示找不到“ -enable-auto-import”,“-enable-runtime-pseudo-reloc”,“-s”,“-subsystem,windos”没有这样的文件或目录。

1)我应该如何调试此问题?

2)我应该尝试使用minGW64版本吗?

------------------------------------编辑---------- ---------------------------

我决定要测试更新版本的QT。因此,我已经安装了QT 5.6.3,并且还使用了QT5.6.3附带的minGW4.9。我还亲自创建了QT项目文件(.pro)。

TEMPLATE = app
TARGET = QT_Simple
INCLUDEPATH += .

# Input
SOURCES += main.cpp

LIBS += -L"C:\Qt\Qt5.6.3\5.6.3\mingw49_32\lib" \
 -L"C:\Qt\Qt5.6.3\5.6.3\mingw49_32\lib" \
 -lmingw32 \
 -lqtmain \
 -lQt5Core \
 -lQt5Gui \

然后我运行“ qmake”来创建Makefile。这是相关的部分。它看起来与以前的qmake没什么不同

####### Compiler, tools and options

CC            = gcc
CXX           = g++
DEFINES       = -DUNICODE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB - 
DQT_NEEDS_QMAIN
CFLAGS        = -pipe -fno-keep-inline-dllexport -O2 -Wall -Wextra 
$(DEFINES)
CXXFLAGS      = -pipe -fno-keep-inline-dllexport -O2 -std=gnu++0x -frtti - 
Wall -Wextra -fexceptions -mthreads $(DEFINES)
INCPATH       = -I. -I. -IC:/Qt/Qt5.6.3/5.6.3/mingw49_32/include - 
IC:/Qt/Qt5.6.3/5.6.3/mingw49_32/include/QtGui - 
IC:/Qt/Qt5.6.3/5.6.3/mingw49_32/include/QtANGLE - 
IC:/Qt/Qt5.6.3/5.6.3/mingw49_32/include/QtCore -Irelease - 
IC:/Qt/Qt5.6.3/5.6.3/mingw49_32/mkspecs/win32-g++ 
LINKER      =        g++
LFLAGS        =        -Wl,-s -Wl,-subsystem,windows -mthreads
LIBS        =        -LC:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib - 
LC:/utils/my_sql/my_sql/lib -LC:/utils/postgresql/pgsql/lib - 
LC:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib -lmingw32 
C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libqtmain.a -lshell32 
C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Core.a 
C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Gui.a 
C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Core.a 
QMAKE         = C:/Qt/Qt5.6.3/5.6.3/mingw49_32/bin/qmake.exe
IDC           = idc
IDL           = midl
...........

我知道这些库文件位于上述目录中(例如libQt5Core.a,libQt5Gui.a等)。但这给了我一个未定义的参考错误。我相信链接器找不到文件。

C:\Users\Administrator\Documents\QT_Test\QT_Simple>mingw32-make
mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/Administrator/Documents/QT_Test/QT_Simple'
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release/QT_Simple.exe release/main.o  -LC:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib
 -LC:/utils/my_sql/my_sql/lib -LC:/utils/postgresql/pgsql/lib -LC:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib -lmingw32 
 C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libqtmain.a -lshell32 C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Core.a 
 C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Gui.a C:/Qt/Qt5.6.3/5.6.3/mingw49_32/lib/libQt5Core.a

release/main.o:main.cpp:(.text+0x4d): undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
release/main.o:main.cpp:(.text+0x7f): undefined reference to `_imp___ZN11QPushButtonC1ERK7QStringP7QWidget'
release/main.o:main.cpp:(.text+0xb0): undefined reference to `_imp___ZN7QWidget6resizeERK5QSize'
release/main.o:main.cpp:(.text+0xbc): undefined reference to `_imp___ZN7QWidget4showEv'
release/main.o:main.cpp:(.text+0xc2): undefined reference to `_imp___ZN12QApplication4execEv'
release/main.o:main.cpp:(.text+0xcd): undefined reference to `_imp___ZN11QPushButtonD1Ev'
release/main.o:main.cpp:(.text+0xd6): undefined reference to `_imp___ZN12QApplicationD1Ev'
release/main.o:main.cpp:(.text+0x120): undefined reference to `_imp___ZN12QApplicationD1Ev'
release/main.o:main.cpp:(.text+0x155): undefined reference to `_imp___ZN11QPushButtonD1Ev'
C:/Qt/Qt5.6.3/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: release/main.o: 
bad reloc address 0x20 in section `.eh_frame'
C:/Qt/Qt5.6.3/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../.
./i686-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Makefile.Release:61: recipe for target 'release/QT_Simple.exe' failed
mingw32-make[1]: *** [release/QT_Simple.exe] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/Administrator/Documents/QT_Test/QT_Simple'
makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2

我不确定为什么找不到引用。是否与不使用DLL文件(libQt5Core.dll等)有关?我是否正确链接?

1 个答案:

答案 0 :(得分:0)

问题出在我的QT项目文件(.pro)中。我添加了以下行,它编译并成功运行。

QT       += core gui widgets

最终的QT_simple.pro文件如下所示

QT       += core gui widgets

TEMPLATE = app
TARGET = QT_Simple
INCLUDEPATH += .

# Input
SOURCES += main.cpp

LIBS += -L"C:\Qt\Qt5.6.3\5.6.3\mingw49_32\lib" \
  -L"C:\Qt\Qt5.6.3\5.6.3\mingw49_32\lib" \
  -lmingw32 \
  -lqtmain \
  -lQt5Core \
  -lQt5Gui \

感谢您的帮助!