Qt项目链接面临未定义的引用

时间:2014-03-04 07:29:32

标签: c++ qt linker

这是.pro文件:

# Add more folders to ship with the application, here
folder_01.source = qml/agritrade
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =

HEADERS += main.hpp
HEADERS += core/util.hpp

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
SOURCES += core/util.cpp

# Installation path
# target.path =

# Please do not modify the following two lines. Required for deployment.
include(qtviewer/qtviewer.pri)
qtcAddDeployment()

这是我的代码的一部分(util.cpp):

/**
 * Get screen dimensions
 */
QRect Core::util::get_screen_dims() {
  return (new QDesktopWidget)->screenGeometry();
}

构建过程中出现错误(编译正常,链接失败):

In function `ZN4Core4util15get_screen_dimsEv':
undefined reference to `_imp___ZN14QDesktopWidgetC1Ev'
undefined reference to `_imp___ZNK14QDesktopWidget14screenGeometryEi'
ld returned 1 exit status

我从qt-project.org下载了Qt 5的整个安装程序以进行安装。在项目链接过程中它有未定义的引用是多么奇怪。

1 个答案:

答案 0 :(得分:1)

您可能在.pro文件中遗漏了这个:

QT += widgets

您创建了一个Qt Quick项目,我认为默认情况下将其保留。如果你混合使用Qt Quick和widgets,你需要自己添加它。

这将告诉 qmake 添加正确的构建路径以包含该模块。在编辑.pro后,请记得运行 qmake (从构建菜单或右键单击项目并从上下文菜单中选择它)。

供参考:qmake manual