Qt Creator Widget项目编译出现undefined reference to _imp_*错误求助
Hey there! Let's tackle those frustrating linker errors you're seeing—undefined reference to __imp__ZN12QApplicationC1ERiPPci and undefined reference to __imp__ZN11QMainWindow11qt_metacastEPKc are classic signs that your project isn't properly linking against the required Qt libraries. Since you've already tried cleaning and rebuilding with no luck, let's dive into the most likely configuration fixes:
1. Check Your .pro File (Critical!)
The most common culprit here is missing the widgets module in your project configuration. Qt 5 and later split widget-related classes (like QApplication and QMainWindow) into a separate widgets module, so you need to explicitly include it.
Open your myWindow.pro file and make sure the QT line includes widgets:
QT += core gui widgets
If it only had core gui before, adding widgets tells Qt to link against the widget library. After saving the file, right-click your project in Qt Creator and select Run qmake (this is crucial—Qt Creator doesn't always auto-detect .pro changes), then rebuild the project.
2. Verify Your Compiler Kit Configuration
Linker errors can also pop up if your Qt version and compiler architecture don't match. For example:
- If you installed a 64-bit Qt version, make sure you're using a 64-bit compiler (e.g., MSVC 2019 64-bit or GCC 64-bit) in your build kit.
- If you're on Windows with MinGW, double-check that the MinGW version matches what Qt was built with.
To check this:
- Go to the Projects tab in Qt Creator.
- Under Build & Run, look at your active kit. Ensure the Qt version, compiler, and debugger all align (no mismatched 32/64 bits).
3. Double-Check Header Includes
While this usually causes compile-time errors (not linker errors), it's worth confirming:
- In
main.cpp, you should have#include <QApplication> - In
mainwindow.h, you should have#include <QMainWindow>
Missing these might not directly cause your current errors, but it's good to rule out any simple oversights.
4. Do a Full Clean (Beyond Qt Creator's "Clean")
Sometimes the build cache gets corrupted in a way that Qt Creator's built-in clean doesn't fix. Try this:
- In the Projects tab, find your Build Directory path.
- Open that folder in your file explorer and delete all its contents.
- Go back to Qt Creator, run qmake again, then rebuild from scratch.
Example Working .pro File
For reference, here's what a basic widget project .pro file should look like:
QT += core gui widgets TARGET = myWindow TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp HEADERS += mainwindow.h
These steps should resolve your linker issues—9 times out of 10, the missing widgets module in the .pro file is the root cause. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者lolad




