qt 5添加 windows 静态库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt 5添加 windows 静态库相关的知识,希望对你有一定的参考价值。

参考技术A qt中添加windows vs 生成的静态库,需要注意:

qt使用的编译器版本要和vs版本,操作系统位数要对应。

例如:用  vs2015 x86生成的了release 和debug两种静态库

相应的QT也要配置vs2015 32 编译器。

把之前vs 生成的静态库的头文件和库准备好

打开qt 中打卡 工程  .pro文件 右键选择 添加库选择外部库

这样我们release和debug版的静态库都添加近工程了。

如果之前已经生成过编译后的文件夹

再次生成时可能遇到 error LNK2019: 无法解析的外部符号 之类的错误信息。

这个问题只要把上面的文件夹删除,从新编译即可。

应用程序如何链接静态QT Plugin库

  1. 错误描述
    静态编译了QT库后,我的应用程序要链接到这些静态库。但是出现了如图所示错误:
    技术分享图片
    运行时错误错误提示:
    This application failed to start because it could not find or load the Qt platform plugin "windows" in "".

  2. 解决过程
    Google关键字“Qt staticPlugins”, to get the first result as:

How to Create Qt Plugins

To link plugins statically, you need to add the required plugins to your build using QTPLUGIN.

In the .pro file for your application, you need the following entry:

QTPLUGIN += qjpeg
qgif
qkrcodecs
qmake automatically adds the plugins to QTPLUGIN that are typically needed by the Qt modules used (see QT), while more specialized plugins need to be added manually. The default list of automatically added plugins can be overridden per type. For example, to link the minimal plugin instead of the default Qt platform adaptation plugin, use:

QTPLUGIN.platforms = qminimal
If you want neither the default, nor the minimal QPA plugin to be linked automatically, use:

QTPLUGIN.platforms = -
The defaults are tuned towards an optimal out-of-the-box experience, but may unnecessarily bloat the application. It is recommended to inspect the linker command line built by qmake and eliminate unnecessary plugins.

Details of Linking Static Plugins
To cause static plugins actually being linked and instantiated, Q_IMPORT_PLUGIN() macros are also needed in application code, but those are automatically generated by qmake and added to your application project.

If you do not want all plugins added to QTPLUGIN to be automatically linked, remove import_plugins from the CONFIG variable:

CONFIG -= import_plugins
Creating Static Plugins
It is also possible to create your own static plugins, by following these steps:

Add CONFIG += static to your plugin‘s .pro file.
Use the Q_IMPORT_PLUGIN() macro in your application.
Use the Q_INIT_RESOURCE() macro in your application if the plugin ships qrc files.
Link your application with your plugin library using LIBS in the .pro file.
See the Plug & Paint example and the associated Basic Tools plugin for details on how to do this.

Note: If you are not using qmake to build your plugin you need to make sure that the QT_STATICPLUGIN preprocessor macro is defined.

3. 解决方法
上述方法均可以使用。我的项目是VC项目,最直接的办法是在main 文件中全局位置调用宏

// link to static QT lib, following lines to import platform static lib‘s header
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)

以上是关于qt 5添加 windows 静态库的主要内容,如果未能解决你的问题,请参考以下文章

Xcode如何添加静态库?

QT静态库静态编译

如何将qt静态库代码还原动态库

QT交叉编译时怎么添加静态库,急!!!

Qt 5.6 5.8 vs2015 编译静态库版本(有全部的截图)good

如何添加预编译静态库 libwebrtc