[英]minGW (g++) Linking Error with QT-qmake auto-generated Makefile in Windows
I am trying to build a simple QT program with MinGW compiler. 我正在尝试使用MinGW编译器构建一个简单的QT程序。 I want to eventually port my Linux developed QT/C++ program in to windows.
我最终希望将我的Linux开发的QT / C ++程序移植到Windows中。 I downloaded QT_4.3.5 (This what my program uses) and MinGW from their site.
我从他们的站点下载了QT_4.3.5(这是我的程序使用的)和MinGW。 I believe this a 32 bit compiler.
我相信这是一个32位编译器。 I am using "cmd" to build the program similar to how I built it in Linux.
我正在使用“ cmd”来构建程序,类似于在Linux中构建程序的方式。 My program is shown below.
我的程序如下所示。
#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();
}
I was able to build and run this program successfully in Linux 5.7. 我能够在Linux 5.7中成功构建并运行该程序。 So, in windows I created a "main.cpp" file and use qmake to create both the .pro file and the Makefile.
因此,在Windows中,我创建了一个“ main.cpp”文件,并使用qmake来创建.pro文件和Makefile。 I am using qmake from the QT4.3.5.
我正在使用QT4.3.5中的qmake。 I used "qmake -project" and qmake.
我使用了“ qmake -project”和qmake。 It successfully built both the .pro file and the Makefile in windows.
它在Windows中成功构建了.pro文件和Makefile。 But when I run make it gives me a "unrecognized commandline option '-Wl' "
但是当我运行make时,它给了我一个“无法识别的命令行选项'-Wl'”
I believe there could be a compatibility issue with qmake version and minGW. 我相信qmake版本和minGW可能存在兼容性问题。 The relevant line in the Makefile is,
Makefile中的相关行是,
LFLAGS = -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows
I then tried to remove -Wl and it gives errors saying can't find "-enable-auto-import", "-enable-runtime-pseudo-reloc", "-s", "-subsystem,windos" no such file or directory. 然后,我尝试删除-Wl并给出错误,提示找不到“ -enable-auto-import”,“-enable-runtime-pseudo-reloc”,“-s”,“-subsystem,windos”,没有此类文件或目录。
1) How should I debug this issue? 1)我应该如何调试此问题?
2) Should I try minGW64 version? 2)我应该尝试minGW64版本吗?
------------------------------------Edited------------------------------------- ------------------------------------编辑------------- ------------------------
I decided to get a newer version of QT to test. 我决定要测试一个较新版本的QT。 So, I have installed QT 5.6.3 and I am also using minGW4.9 that come with QT5.6.3.
因此,我已经安装了QT 5.6.3,并且还使用了QT5.6.3附带的minGW4.9。 I have also created the QT project file (.pro) myself.
我还自己创建了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 \
Then I run "qmake" to create the Makefile. 然后我运行“ qmake”来创建Makefile。 Here's the relevant sections.
这是相关的部分。 It looks little different from the previous qmake
它看起来与以前的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
...........
I know the library files are in the above directories (ex. libQt5Core.a, libQt5Gui.a, etc). 我知道库文件位于上述目录中(例如libQt5Core.a,libQt5Gui.a等)。 But it gives me an udefined reference error.
但这给了我一个未定义的参考错误。 I believe the linker can't find the files.
我相信链接器找不到文件。
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
I am not sure why it can't find the references. 我不确定为什么找不到引用。 Does it have to do with not using the DLL files (libQt5Core.dll, etc).
是否与不使用DLL文件(libQt5Core.dll等)有关? Am I linking properly?
我是否正确链接?
The issue was with my QT project file (.pro). 问题出在我的QT项目文件(.pro)中。 I added the following line and it compiled and ran successfully.
我添加了以下行,它编译并成功运行。
QT += core gui widgets
The final QT_simple.pro file looks like the following, 最终的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 \
Thanks for the help! 谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.