免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9115 | 回复: 12
打印 上一主题 下一主题

Qt 编译hello程序 undefined reference 出错 求助! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-12 11:08 |只看该作者 |倒序浏览
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

论坛徽章:
0
2 [报告]
发表于 2010-11-12 11:56 |只看该作者
回复 1# zhangyue8624

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

论坛徽章:
0
3 [报告]
发表于 2010-11-12 16:47 |只看该作者
回复 2# cstdio


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

论坛徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
4 [报告]
发表于 2010-11-12 17:08 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2010-11-12 17:15 |只看该作者
ls说的对,你qt库都没链接进去。

论坛徽章:
0
6 [报告]
发表于 2010-11-12 17:23 |只看该作者
回复 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

论坛徽章:
0
7 [报告]
发表于 2010-11-12 17:24 |只看该作者
回复 5# cstdio


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

论坛徽章:
0
8 [报告]
发表于 2010-11-12 17:34 |只看该作者
回复 7# zhangyue8624
-L/qtlib-path -lQtGui -lQtCore

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

论坛徽章:
0
9 [报告]
发表于 2010-11-12 18:14 |只看该作者
回复 8# cstdio


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

论坛徽章:
0
10 [报告]
发表于 2010-11-13 11:46 |只看该作者
qmake -project
qmake
make
这样最简单了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP