Qt5 linking error

2019-09-09 20:31发布

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

标签: c++ linux gcc qt5
1条回答
劫难
2楼-- · 2019-09-09 20:47

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.

查看更多
登录 后发表回答