Qt 编译hello程序 undefined reference 出错 求助!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
qmake -project
qmake
make
这样最简单了。
回复 8# cstdio
我就是用progen产生的hello.pro文件,然后用tmake产生的makefile,但我不太清楚了需要的库在哪里加上,不会是每次有新工程都要手动加到makefile里面吧。。。
回复 7# zhangyue8624
-L/qtlib-path -lQtGui -lQtCore
既然是qt工程,你还是用.pro组织文件,方便又省心。
回复 5# cstdio
请问如何把库链接进去呢,我想用QT-x11的库
回复 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
ls说的对,你qt库都没链接进去。
回复 2# cstdio
我用qt-x11-2.3.2,头文件没问题,错误依旧。
回复 1# zhangyue8624
记得qt4头文件包含应该是
#include <QApplication>
#include <QLabel>