0

I have just installed Qt5 on my system (linux mint petra) by installing the package qt5-default. I have a simple .ui file and a main.cpp. Using uic, I can translate my .ui file into a .h file, which is included by main.cpp. No problem until now.

I run qmake -project, qmake and make. Compiling is just fine, I get main.o. But linking gives me a bunch of "undefined references...".

So, I checked the libraries. This is the linker call:

g++ -m64 -Wl,-O1 -o qttest main.o   -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lGL -lpthread

Ok, so I searched for the libraries. As far as I know, the parameter -lQt5Core forces the linker to look for a file with name libQt5Core.a, in the directories specified with the -L option. However, this folder only contains a libQt5core.so. Same thing with the other needed libraries. As far as I know, .a files are for static linking while .so is for dynamic linking.

Now, how should I proceed? Should I search the internet for a .a library? Why is qmake generating a makefile which tries so static link? Am I missing some packages? I haven't ever dynamically linked. Do I have to add code for loading the .so? I have the feeling statically linking is easier as a first step.

Best Regards

lugge86
  • 255
  • 3
  • 11
  • what are the undefined references? It would be useful to know if they're actually in Qt5Core or somewhere else (gcc is perfectly capable of handling dynamic libs for you). – Useless May 08 '14 at 15:46

1 Answers1

3

Using uic, I can translate my .ui file into a .h file

No, you're supposed to let your buildsystem do that. I.e. use the FORMS variable in your .pro file. Or are you willing to rerun uic manually every time you touch your .ui?

As far as I know, the parameter -lQt5Core forces the linker to look for a file with name libQt5Core.a, in the directories specified with the -L option. However, this folder only contains a libQt5core.so. Same thing with the other needed libraries. As far as I know, .a files are for static linking while .so is for dynamic linking.

That's only half of the story. On systems that support shared libraries (i.e. ALL modern systems), GNU ld will look first for shared libraries (i.e. .so), and only if a shared library isn't found then it'll look around for static libraries (.a).

Anyhow, that's not your issue. Your issue is that you're using .ui files, that is, widgets, and you're not telling qmake that you want the QtWidgets library, because there's no -lQtWidgets in the linker's command line.

Solution: add

QT += widgets

to your .pro file, rerun qmake, make, that's it.

peppe
  • 21,934
  • 4
  • 55
  • 70
  • Thank you peppe, that perfectly solved all my problems. The reason why I ran uic manually was because my makefile was missing this call, so the header wasn't generated. Even this is solved by your answer. Ok, I have to read about this. One more question: my .pro file was generated by qmake -project. Is there any reason why it did not add this one line to the .pro file? The .ui file has been recognized and added to FORMS, but the "widgets" part was missing. – lugge86 May 08 '14 at 16:35
  • Because by default only the `core` and the `gui` modules are compiled in. You can see from your command line that those are the two libs your program tries to link to. Hence, `FORMS` has no special meaning (it needs `widgets`). – peppe May 08 '14 at 21:41