• 25839阅读
  • 18回复

Qt4书中第一个例子,找不到QApplication头文件的问题。有些急。 [复制链接]

上一主题 下一主题
离线zhenzhong
 
只看楼主 倒序阅读 楼主  发表于: 2007-01-26
在做《C++ GUI Programming with Qt 4》书中的第一个例子时就被卡到了。Qt版本为4.2.0。源码安装。Ubuntu Edgy。网上搜索了很多地方,现在我没有解决这个问题。谢谢。

程序源码:

  1. #include <QApplication>
  2. #include <QLabel>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. QLabel *label = new QLabel("Hello Qt!");
  7. label->show();
  8. return app.exec();
  9. }


vim+gcc执行

  1. :!gcc -Wall main.c -o main


得到错误:

  1. main.c:1:25: error: QApplication: No such file or directory
  2. main.c:2:19: error: QLabel: No such file or directory
  3. main.c: In function ‘int main(int, char**)’:
  4. main.c:9: error: ‘QApplication’ was not declared in this scope
  5. main.c:9: error: expected `;' before ‘app’
  6. main.c:10: error: ‘QLabel’ was not declared in this scope
  7. main.c:10: error: ‘label’ was not declared in this scope
  8. main.c:10: error: expected type-specifier before ‘QLabel’
  9. main.c:10: error: expected `;' before ‘QLabel’
  10. main.c:13: error: ‘app’ was not declared in this scope
  11. shell returned 1
  12. Press ENTER or type command to continue


原因可能是环境变量没有设置好,但我看自己的环境变量是设置好了的。如下:

  1. alex@warsaw:~/temp/Qt_test$ env | grep QTDIR
  2. QTDIR=/usr/local/qt


上面已经包括了/usr/local/qt路径,但是怎么提示说找不到QApplication这个头文件呢。我该如何写环境变量,什么名字?才能每次调用gcc都能正确编译呢?

如果您做过Qt应用,知道如何解决,给我些提示吧,大拿们回复一下。我比较急。谢谢。:)
[ 此贴被zhenzhong在2007-01-26 19:41重新编辑 ]
离线ningzhi
只看该作者 1楼 发表于: 2007-02-01
执行
qmake -project
qmake
make
注意把$QTDIR/bin添加到PATH里 $QTINC $QTLIB 分别置成 $QTDIR/include
$QTDIR/lib  如果手动写可以先看看自动生成的makefile中是怎么设置-L -I -l的 :)
// 地球人,你们好。。。
离线forestlier

只看该作者 2楼 发表于: 2007-02-05
头文件包含应该是:
#include <qapplication.h>
#include <qlabel.h>
离线chwoozy

只看该作者 3楼 发表于: 2007-02-27
3楼的说的是Qt3的写法,到了Qt4里面头文件的写法已经有变化了
离线zhouyangc
只看该作者 4楼 发表于: 2007-03-21
应该还是环境变量的问题
搞得懂就答人,搞不懂就问人,没有人懂还可以问神!
离线浪漫天使
只看该作者 5楼 发表于: 2007-03-22
因为qt3 以前的 include目录里面就直接是头文件了,而qt4的include目录底下还有目录才到头文件,但是qt4的pro出来的makefile的路径只到QTDIR/include 所以qt4的头文件最好是写成
#include <QtGui/***>
#include <Qt/***>
意思就是包含一下include中子目录的名字,再到头文件.
离线gingerlee
只看该作者 6楼 发表于: 2007-07-21
^_^,支持
离线xzh_biti
只看该作者 7楼 发表于: 2008-05-08
5楼正解
离线steinlee

只看该作者 8楼 发表于: 2008-05-16
The problem is that include path is not set. Maybe your library path is not set either. Add the following to the shell(file .bashrc):
export QTDIR=/usr/local/qt
export PATH=$QTDIR/bin:.:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR/lib

Then you will be fine.
Looking for remote C/C++ and Qt 兼职
离线linwalker
只看该作者 9楼 发表于: 2008-05-18
用gcc不行的话就直接用QT自带的qmake吧,这个方便多了
离线wd007

只看该作者 10楼 发表于: 2008-12-13
5楼正解
欢迎访问我的博客,一起学习提高
http://blog.csdn.net/qter_wd007
离线yzy727

只看该作者 11楼 发表于: 2008-12-17
可能是QMAKESPEC没设置好;
参考http://user.qzone.qq.com/94386891?ptlang=2052
成就一番事业,早点结婚~~
离线steinlee

只看该作者 12楼 发表于: 2009-01-24
Your mistakes in this command: gcc -Wall main.c -o main

  1. no library link
  2. no include path

Learn some things about make. Type "how to write make files" in google
[ 此贴被steinlee在2009-01-24 00:34重新编辑 ]
Looking for remote C/C++ and Qt 兼职
离线bianxuefen
只看该作者 13楼 发表于: 2009-02-17
如果执行以下命令:
qmake -project
qmake
make
仍有错误,可能是qmake版本问题,可以用这样的命令:
qmake-qt4  -project
qmake-qt4
make
即可解决,并生成与项目名匹配的可执行文件,之后执行方式如:./xxx(xxx为刚生成的可执行文件名)
相信自己
离线wd007

只看该作者 14楼 发表于: 2009-02-18
楼上是有道理的,楼主可能同时安装了Qt3和Qt4,而qmake默认指向Qt3的qmake
欢迎访问我的博客,一起学习提高
http://blog.csdn.net/qter_wd007
离线liuyong_401
只看该作者 15楼 发表于: 2009-02-25
支持 bianxuefen   我的情况就是那样 呵呵 找了半天的问题 终于解决了
离线grissom
只看该作者 16楼 发表于: 2009-06-24
#include <QtGui/QApplication>
#include <QtGui/QLabel>
离线wking1986
只看该作者 17楼 发表于: 2010-01-20
各位大侠,我的问题与楼主的问题一样,我也按着大家的方法试了,可是还是不好是,咳咳。。希望大侠们再指点一下!!!
离线xphcyh
只看该作者 18楼 发表于: 2010-08-04
快速回复
限100 字节
 
上一个 下一个