Qt5 linking error

让人想犯罪 __ 提交于 2019-12-13 22:07:39

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23546474/qt5-linking-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!