简体   繁体   English

Qt5链接错误

[英]Qt5 linking error

I have just installed Qt5 on my system (linux mint petra) by installing the package qt5-default. 我刚刚安装了qt5-default软件包,在我的系统(linux mint petra)上安装了Qt5。 I have a simple .ui file and a main.cpp. 我有一个简单的.ui文件和一个main.cpp。 Using uic, I can translate my .ui file into a .h file, which is included by main.cpp. 使用uic,我可以将.ui文件转换为.h文件,该文件包含在main.cpp中。 No problem until now. 直到现在都没问题。

I run qmake -project, qmake and make. 我运行qmake -project,qmake和make。 Compiling is just fine, I get main.o. 编译很好,我得到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. 据我所知,参数-lQt5Core强制链接器在使用-L选项指定的目录中查找名为libQt5Core.a的文件。 However, this folder only contains a libQt5core.so. 但是,此文件夹仅包含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. 据我所知,.a文件用于静态链接,而.so用于动态链接。

Now, how should I proceed? 现在,我该怎么办? Should I search the internet for a .a library? 我应该在互联网上搜索.a库吗? Why is qmake generating a makefile which tries so static link? 为什么qmake会生成一个尝试静态链接的makefile? Am I missing some packages? 我错过了一些包吗? I haven't ever dynamically linked. 我没有动态链接。 Do I have to add code for loading the .so? 我是否必须添加用于加载.so的代码? I have the feeling statically linking is easier as a first step. 作为第一步,我感觉静态链接更容易。

Best Regards 最好的祝福

Using uic, I can translate my .ui file into a .h file 使用uic,我可以将.ui文件转换为.h文件

No, you're supposed to let your buildsystem do that. 不,你应该让你的构建系统做到这一点。 Ie use the FORMS variable in your .pro file. 即在.pro文件中使用FORMS变量。 Or are you willing to rerun uic manually every time you touch your .ui ? 或者你是否愿意每次触摸你的.ui时手动重新运行uic?

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. 据我所知,参数-lQt5Core强制链接器在使用-L选项指定的目录中查找名为libQt5Core.a的文件。 However, this folder only contains a libQt5core.so. 但是,此文件夹仅包含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. 据我所知,.a文件用于静态链接,而.so用于动态链接。

That's only half of the story. 这只是故事的一半。 On systems that support shared libraries (ie ALL modern systems), GNU ld will look first for shared libraries (ie .so ), and only if a shared library isn't found then it'll look around for static libraries ( .a ). 在支持共享库(即所有现代系统)的系统上,GNU ld将首先查找共享库(即.so ),并且只有在找不到共享库时才会查找静态库( .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. 您的问题是您正在使用.ui文件,即小部件,并且您没有告诉qmake您想要QtWidgets库,因为链接器的命令行中没有-lQtWidgets

Solution: add 解决方案:添加

QT += widgets

to your .pro file, rerun qmake, make, that's it. 到你的.pro文件,重新运行qmake,make,就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM