FunnyHorse FunnyHorse - 3 years ago 184
C++ Question

GCC can not recognize Qt libraries or/and include files

I am trying to compile my Qt app from console. Here is specs:
MCBC 3.0 (has preinstalled Qt and gcc), Qt 4.6.4, gcc v4.1.2, qmake v2.01a.

My test qt app contains only 2 files: and main.cpp.

TARGET = basket
SOURCES += main.cpp


#include <QtGui/QApplication>

int main(int argc, char *argv[])
QApplication a(argc, argv);
return a.exec();

I need to compile that app from console.

I got 2 virtual machines. VM #1 has installed QtCreator, and QtCreator compile this app without troubles, also app can be compiled from the console by "qmake" and "make". But there is VM#2, which does not have installed QtCreator, and it is where problems come from.
I tried compile app by commands "qmake make", trying to use Makefile, created by QtCreator, trying to use only gcc comands with explicitly defined paths to Qt libs, include-files and linked libs, but always I got the same errors. For example, I use 2 gcc comands, which used by QtCreator for successful compile this app:

g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib/qt46/mkspecs/linux-g++ -I. -I/usr/lib/qt46/include/QtCore -I/usr/lib/qt46/include/QtGui -I/usr/lib/qt46/include -I. -o main.o main.cpp

g++ -o basket main.o -L/usr/lib/qt46/lib -lQtGui -L/usr/lib/qt46/lib -lQtCore -lpthread

And so after executing second command I got same error as always:

[root@ARM basket]# ./build_g++_step2
main.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::data(void) const':
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::QApplication(int &, char **, int)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::exec(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
collect2: ld returned 1 exit status

Please tell what I missed.

Answer Source

Your GCC compiler is version 2.95 (a much too old version from the previous century, which is not conforming to C++11 standard, and which was known to be quite buggy), since /usr/lib/gcc-lib/i586-linux/2.95.4/is used. Run simply g++ -v or g++ --version to check the version of your compiler. (You could have some PATH issue, or you could have misinstalled your build tools).

You should upgrade your compiler to some much newer version (at least GCC 6 and preferably GCC 7 in july 2017)

BTW, I recommend upgrading also your Qt library. Current version (in july 2017) is Qt5.9 and you should use some Qt5.

Then you need to regenerate your makefile, adding more in QT, as answered by ilbeldus

Notice that you could use pkg-config to help compiling Qt applications (perhaps in your Makefile). You still need to explicitly use moc. For example you might compile some Qt C++ GUI file (after having used moc) with e.g.

 g++ -Wall -g -c $(pkg-config --cflags Qt5Gui) foo.c

and you would use $(pkg-config --libs Qt5Gui) for link options.

I don't recommend using your old MCBC thing (even with googling I didn't find what that is), notably if it is so obsolete.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download