3 Replies - 2686 Views - Last Post: 04 December 2011 - 01:14 PM Rate Topic: -----

#1 LastElemental  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 11

Problem compiling C++ and Qt code

Posted 04 December 2011 - 09:45 AM

Hello everybody. I am trying to develop a program for a club in my high school using C++ and QT, however I keep getting errors about undefined references to QT methods:

/usr/lib/gcc/i686-pc-linux-gnu/4.6.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cckJ86t3.o: In function `form(int, char**)':
gui2.cpp:(.text+0x29): undefined reference to `QApplication::QApplication(int&, char**, int)'
gui2.cpp:(.text+0x56): undefined reference to `QWidget::QWidget(QWidget*, QFlags<Qt::WindowType>)'
gui2.cpp:(.text+0xa7): undefined reference to `QCoreApplication::translate(char const*, char const*, char const*, QCoreApplication::Encoding)'
gui2.cpp:(.text+0xbc): undefined reference to `QWidget::setWindowTitle(QString const&)'
gui2.cpp:(.text+0xf2): undefined reference to `QCoreApplication::translate(char const*, char const*, char const*, QCoreApplication::Encoding)'
gui2.cpp:(.text+0x11a): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x134): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)'
gui2.cpp:(.text+0x14e): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x160): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x172): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x184): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x196): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x1a8): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x1ba): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x1cc): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x1e8): undefined reference to `QFormLayout::QFormLayout(QWidget*)'
gui2.cpp:(.text+0x222): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x26a): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x2b2): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x2fa): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x342): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x359): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x36b): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x37d): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x38f): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x3a1): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x3b3): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x3c5): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x3d7): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x3e9): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x3fb): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x40d): undefined reference to `operator new(unsigned int)'
gui2.cpp:(.text+0x41f): undefined reference to `QLineEdit::QLineEdit(QWidget*)'
gui2.cpp:(.text+0x462): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x4aa): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x4f2): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x53a): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
gui2.cpp:(.text+0x582): undefined reference to `QFormLayout::addRow(QString const&, QWidget*)'
/tmp/cckJ86t3.o:gui2.cpp:(.text+0x5ca): more undefined references to `QFormLayout::addRow(QString const&, QWidget*)' follow
/tmp/cckJ86t3.o: In function `form(int, char**)':
gui2.cpp:(.text+0x5e3): undefined reference to `QFormLayout::~QFormLayout()'
gui2.cpp:(.text+0x5ee): undefined reference to `QWidget::~QWidget()'
gui2.cpp:(.text+0x5f9): undefined reference to `QApplication::~QApplication()'
gui2.cpp:(.text+0x61d): undefined reference to `operator delete(void*)'
gui2.cpp:(.text+0x63d): undefined reference to `operator delete(void*)'
gui2.cpp:(.text+0x64e): undefined reference to `operator delete(void*)'
gui2.cpp:(.text+0x65f): undefined reference to `operator delete(void*)'
gui2.cpp:(.text+0x670): undefined reference to `operator delete(void*)'
/tmp/cckJ86t3.o:gui2.cpp:(.text+0x6db): more undefined references to `operator delete(void*)' follow
/tmp/cckJ86t3.o: In function `form(int, char**)':
gui2.cpp:(.text+0x798): undefined reference to `QFormLayout::~QFormLayout()'
gui2.cpp:(.text+0x7a7): undefined reference to `QWidget::~QWidget()'
gui2.cpp:(.text+0x7b6): undefined reference to `QApplication::~QApplication()'
/tmp/cckJ86t3.o: In function `QString::~QString()':
gui2.cpp:(.text._ZN7QStringD2Ev[_ZN7QStringD5Ev]+0x23): undefined reference to `QString::free(QString::Data*)'
/tmp/cckJ86t3.o: In function `QObject::tr(char const*, char const*)':
gui2.cpp:(.text._ZN7QObject2trEPKcS1_[QObject::tr(char const*, char const*)]+0x1b): undefined reference to `QObject::staticMetaObject'
gui2.cpp:(.text._ZN7QObject2trEPKcS1_[QObject::tr(char const*, char const*)]+0x23): undefined reference to `QMetaObject::tr(char const*, char const*) const'
/tmp/cckJ86t3.o: In function `QWidget::resize(int, int)':
gui2.cpp:(.text._ZN7QWidget6resizeEii[QWidget::resize(int, int)]+0x2d): undefined reference to `QWidget::resize(QSize const&)'
/tmp/cckJ86t3.o:(.eh_frame+0xef): undefined reference to `__gxx_personality_v0'


from this code:

#include <QtCore>
#include <QtGui>

public class userinterface
{
	QLabel *autoLabel, *autoqLabel;
	public void create(int argc, char *argv[])
	{
		QApplication app(argc, argv);
		QWidget window;
		window.resize(300,300);
		window.show;
		
		QGridLayout *autoteleLayout = new QGridLayout(window);
		
		autoLabel = new QLabel("AUTO", autoteleLayout, 0);
		autoteleLayout.addWidget(autoLabel, 0,0,0);
		
		autoqLabel = new QLabel("?", autoteleLayout, 0);
		autoteleLayout.addWidget(autoqLabel,0,0,0);
		
		
	}
}


Does anyone know why I am getting this or can help me solve my issues?

Wow I'm stupid I forgot to change the file I was testing. Nevermind.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem compiling C++ and Qt code

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Problem compiling C++ and Qt code

Posted 04 December 2011 - 10:57 AM

How are you compiling the project? The best way to compile QT is to use qmake, which should generate a Makefile. My qmake is called qmake-qt4.
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

Re: Problem compiling C++ and Qt code

Posted 04 December 2011 - 11:20 AM

To point you in the right direction.

These are linking errors to Qt, not compilation errors. This means that there probably nothing actually wrong with your code. But after your code has compiled, the linking process looks for an implementation for every function you use and in this case he doesn't find any implementation for the Qt functions.

I see this is linux. If you compile by hand then you must point it to the qt library the program needs and ofcourse the library needs to be installed.

In ubuntu the Qt4 development files are in the package libqt4-dev.

Then

You should add 3 kinds of flags. -Ipath, signifies it should look for include files in a particular directory. -Lpath means it should look for library files in a particular directory. -lname means it should include a particular library (use -lname to include libname.so).

For your file it should look something like this.

g++ -o test.o -c test.cpp -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore -lQtCore -lQtGui



Now how did I found out all those flags.

Most development packages come with a pkg-config file (.pc) that states what the correct compiler and link flags are. So basically I just asked the tool pkg-config to list the flags like this.

pkg-config QtGui --libs --cflags

To get rid of this mess, people created scripts, when the scripts became cumbersome, they designed Makefiles. When that became cumbersome, then they started auto generating makefiles.

So in a real project you will probably want to use one of those automake/cmake are the most popular to gather/verify dependencies and compile projects.

This post has been edited by Karel-Lodewijk: 04 December 2011 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 LastElemental  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 11

Re: Problem compiling C++ and Qt code

Posted 04 December 2011 - 01:14 PM

Thanks for the information I was using a bash file with this in it:
gcc -I/usr/include/Qt -I/usr/include/QtGui -I/usr/include/QtCore test.cpp


And I am doing this through gcc just because I don't plan on making this available outside of the club until after it's finished, tested, and approved of by the club, but I will look into using makefiles
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1