【问题标题】:minGW (g++) Linking Error with QT-qmake auto-generated Makefile in WindowsminGW (g++) 在 Windows 中与 QT-qmake 自动生成的 Makefile 链接错误
【发布时间】:2019-02-01 18:54:42
【问题描述】:

我正在尝试使用 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 等)有关。我是否正确链接?

【问题讨论】:

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


【解决方案1】:

问题出在我的 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 \

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多