从QT Wiki构建项目失败:未解析外部符号及构建报错
Hey there, let's work through this Qt build problem you're facing. From what you described, you copied code from a Qt beginner Wiki, tried to build, got errors mentioning your .pro file and source files, plus unresolved external symbols—even after cleaning and rebuilding. Here's how to troubleshoot this step by step:
1. Verify Your .pro File Configuration
First, the error listing testempty.pro window.h window.cpp main.cpp suggests qmake might not be correctly set up to compile your files. Double-check your .pro file to make sure it includes all your source and header files, plus the necessary Qt modules:
# Basic Qt module includes (adjust based on Qt version) QT += core gui # For Qt 5+, add widgets if you're using QWidget-based classes greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = testempty TEMPLATE = app # List all your source files here SOURCES += main.cpp \ window.cpp # List all your header files here HEADERS += window.h
If the Wiki code didn't include a proper .pro template, this is likely the root cause—qmake needs explicit instructions on which files to compile and which Qt libraries to link against.
2. Fix Unresolved External Symbols
Unresolved external symbols almost always link to a mismatch between declarations and implementations, or missing library links:
- Check class implementation matching: Make sure every function declared in
window.hhas a corresponding implementation inwindow.cppwith the correct class prefix. For example, if your header hasclass Window : public QWidget { public: Window(QWidget *parent = nullptr); };, your cpp should have:
Forgetting theWindow::Window(QWidget *parent) : QWidget(parent) { // Your code here }Window::prefix or mismatching parameters will cause link errors. - Ensure Qt modules are linked: If you're using Qt 5+ and widgets like
QWidget,QPushButton, etc., your.promust haveQT += widgets(Qt 4 usesQT += guiinstead). Missing this means the linker can't find Qt's widget library symbols. - Clear stubborn build cache: Sometimes
Clean Alldoesn't wipe everything. Try deleting the entirebuild-testempty-xxxfolder (the auto-generated build directory in your project folder), then right-click your project in Qt Creator → Run qmake → Build All.
3. Double-Check Compiler Output
Take a closer look at the full error message from the compiler/linker. The specific unresolved symbol name (e.g., Window::Window() or QWidget::QWidget()) will tell you exactly what's missing—whether it's your own class implementation or a Qt library reference.
内容的提问来源于stack exchange,提问作者Corvus

