• 8005阅读
  • 8回复

怎么我编译的时候出现这样的错误? [复制链接]

上一主题 下一主题
离线chwoozy
 

只看楼主 倒序阅读 楼主  发表于: 2006-04-09
从官方文档里拷下来了一段“Helloworld”的代码:
  1. #include <qapplication.h>
  2. #include <qpushbutton.h>
  3. int main( int argc, char **argv )
  4. {
  5.   QApplication a(argc, argv);
  6.   QPushButton hello( "Hello world!", 0 );
  7.   hello.resize( 100, 30 );
  8.   a.setMainWidget( &hello );
  9.   hello.show();
  10.   return a.exec();
  11. }

保存为main.cpp,然后用qmake -project
                        qmake
来生成makefile,这两步都没有报错
但是在make的时候出错了:
  1. [root@tju-cch qt]# make
  2. g++ -c -pipe -Wall -W -O2 -g -pipe -D_FORTIFY_SOURCE=2 -m32 -march=i686 -mcpu=i686 -mtune=i686 -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o main.o main.cpp
  3. g++ -o qt main.o   -L/usr/X11R6/lib -lXext -lX11 -lm
  4. main.o: In function `main':
  5. /home/chwoozy/qt/main.cpp:7: undefined reference to `QApplication::QApplication(int&, char**)'
  6. /home/chwoozy/qt/main.cpp:9: undefined reference to `QString::QString(char const*)'
  7. /home/chwoozy/qt/main.cpp:9: undefined reference to `QPushButton::QPushButton(QString const&, QWidget*, char const*)'
  8. main.o: In function `main':
  9. /usr/lib/qt-3.3/include/qstring.h:847: undefined reference to `QString::shared_null'
  10. /usr/lib/qt-3.3/include/qstring.h:848: undefined reference to `QStringData::deleteSelf()'
  11. main.o: In function `main':
  12. /home/chwoozy/qt/main.cpp:10: undefined reference to `QPushButton::resize(int, int)'
  13. /home/chwoozy/qt/main.cpp:12: undefined reference to `QApplication::setMainWidget(QWidget*)'
  14. /home/chwoozy/qt/main.cpp:13: undefined reference to `QWidget::show()'
  15. /home/chwoozy/qt/main.cpp:14: undefined reference to `QApplication::exec()'
  16. /home/chwoozy/qt/main.cpp:14: undefined reference to `QPushButton::~QPushButton()'
  17. /home/chwoozy/qt/main.cpp:14: undefined reference to `QApplication::~QApplication()'
  18. main.o: In function `main':
  19. /usr/lib/qt-3.3/include/qstring.h:847: undefined reference to `QString::shared_null'
  20. /usr/lib/qt-3.3/include/qstring.h:848: undefined reference to `QStringData::deleteSelf()'
  21. main.o: In function `main':
  22. /home/chwoozy/qt/main.cpp:14: undefined reference to `QApplication::~QApplication()'
  23. /home/chwoozy/qt/main.cpp:14: undefined reference to `QPushButton::~QPushButton()'
  24. main.o:(.gnu.linkonce.r._ZTV6QGList+0xc): undefined reference to `QGList::clear()'
  25. main.o:(.gnu.linkonce.r._ZTV6QGList+0x10): undefined reference to `QGList::~QGList()'
  26. main.o:(.gnu.linkonce.r._ZTV6QGList+0x14): undefined reference to `QGList::~QGList()'
  27. main.o:(.gnu.linkonce.r._ZTV6QGList+0x18): undefined reference to `QPtrCollection::newItem(void*)'
  28. main.o:(.gnu.linkonce.r._ZTV6QGList+0x20): undefined reference to `QGList::compareItems(void*, void*)'
  29. main.o:(.gnu.linkonce.r._ZTV6QGList+0x24): undefined reference to `QGList::read(QDataStream&, void*&)'
  30. main.o:(.gnu.linkonce.r._ZTV6QGList+0x28): undefined reference to `QGList::write(QDataStream&, void*) const'
  31. main.o:(.gnu.linkonce.r._ZTI6QGList+0x8): undefined reference to `typeinfo for QPtrCollection'
  32. collect2: ld returned 1 exit status
  33. make: *** [qt] 错误 1

不知道是哪里出错了?
我用的是MagicLinux2.0正式版里面自带的Qt3.3.6
[ 此贴被chwoozy在2006-04-09 17:32重新编辑 ]
离线chwoozy

只看该作者 1楼 发表于: 2006-04-09
需不需要我把Makefile也帖上来?
离线XChinux

只看该作者 2楼 发表于: 2006-04-09
g++ -o qt main.o   -L/usr/X11R6/lib -lXext -lX11 -lm

这一句似乎表明了没有找到动态库来链接。
你需要把QT3的lib库的路径加入到lD_LIBRARY_PATH变量中去。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线chwoozy

只看该作者 3楼 发表于: 2006-04-09
很奇怪,MagicLinux2.0正式版中没有这个lD_LIBRARY_PATH变量。我手动设了一下,即在Shell里运行
  1. lD_LIBRARY_PATH=/usr/lib/qt-3.3/lib

后用make时问题依旧。我怀疑是不是makefile里有句:
  1. LIBS = $(SUBLIBS) -L/usr/X11R6/lib -lXext -lX11 -lm

是这句设置变量的语句引起的问题吧
离线XChinux

只看该作者 4楼 发表于: 2006-04-09
那你手工在.pro里加上关于库的路径。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线chwoozy

只看该作者 5楼 发表于: 2006-04-09
很奇怪我以前make附件中的这个项目的时候就没事
附件: C++ GUI Programming with Qt 3 - examples.zip (394 K) 下载次数:8
离线XChinux

只看该作者 6楼 发表于: 2006-04-09
或者你修改一下/etc/ld.so.conf这个文件,把QT3的库路径加入进去。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线chwoozy

只看该作者 7楼 发表于: 2006-04-09
虽然修改/etc/ld.so.conf这个方法无效,但是改makefile,在LIBS变量加上一个-lqt-mt参数好像就行了。谢谢总版主了
离线chwoozy

只看该作者 8楼 发表于: 2006-04-10
很奇怪现在再qmake -project && qmake && make的时候不会出错了
快速回复
限100 字节
 
上一个 下一个