Qt 编译hello程序 undefined reference 出错 求助!

发布于 2022-09-10 18:07:11 字数 3333 浏览 34 评论 8

hello.cpp程序如下:

#include <qapplication.h>
#include <qlabel.h>

int main(int argc, char **argv)
{
        QApplication app(argc, argv);

        QLabel *label = new QLabel("Hello, Qt!", 0);
        label->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
        label->setGeometry(10, 10, 200, 80);
        app.setMainWidget(label);
        label->show();

        int result = app.exec();
        return result;
}
~

编译时错误如下:
[root@localhost hell]# make
gcc  -o hello hello.o  
hello.o: In function `main':
hello.cpp.text+0x31): undefined reference to `QApplication:Application(int&, char**)'
hello.cpp.text+0x41): undefined reference to `QString:String(char const*)'
hello.cpp.text+0x4d): undefined reference to `operator new(unsigned int)'
hello.cpp.text+0x73): undefined reference to `QLabel:Label(QString const&, QWidget*, char const*, unsigned int)'
hello.cpp.text+0x8d): undefined reference to `QString::shared_null'
hello.cpp.text+0x99): undefined reference to `QString::shared_null'
hello.cpp.text+0xa1): undefined reference to `QStringData::deleteSelf()'
hello.cpp.text+0xeb): undefined reference to `QApplication::setMainWidget(QWidget*)'
hello.cpp.text+0xfe): undefined reference to `QApplication::exec()'
hello.cpp.text+0x10: undefined reference to `QApplication::~QApplication()'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

江南月 2022-09-12 01:19:45

qmake -project
qmake
make
这样最简单了。

两相知 2022-09-12 01:18:52

回复 8# cstdio

    我就是用progen产生的hello.pro文件,然后用tmake产生的makefile,但我不太清楚了需要的库在哪里加上,不会是每次有新工程都要手动加到makefile里面吧。。。

唯憾梦倾城 2022-09-12 01:14:07

回复 7# zhangyue8624
-L/qtlib-path -lQtGui -lQtCore

既然是qt工程,你还是用.pro组织文件,方便又省心。

三生一梦 2022-09-12 01:13:26

回复 5# cstdio

    请问如何把库链接进去呢,我想用QT-x11的库

不打扰别人 2022-09-12 01:11:50

回复 4# c/unix

    我产生的makefile文件如下,里面有链接库,QTDIR我设的是QT-x11的库

####### Compiler, tools and options

CC      =       gcc
CXX     =       g++
CFLAGS  =       -pipe -Wall -W -O2 -DNO_DEBUG
CXXFLAGS=       -pipe -DQWS -fno-exceptions -fno-rtti -Wall -W -O2 -DNO_DEBUG
INCPATH =       -I$(QTDIR)/include
LINK    =       gcc
LFLAGS  =
LIBS    =       $(SUBLIBS) -L$(QTDIR)/lib -lqte
MOC     =       $(QTDIR)/bin/moc
UIC     =       $(QTDIR)/bin/uic

TAR     =       tar -cf
GZIP    =       gzip -9f

游魂 2022-09-11 23:42:41

ls说的对,你qt库都没链接进去。

蒗幽 2022-09-11 12:15:21

回复 2# cstdio

    我用qt-x11-2.3.2,头文件没问题,错误依旧。

左耳近心 2022-09-11 03:01:12

回复 1# zhangyue8624

记得qt4头文件包含应该是
#include <QApplication>
#include <QLabel>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文