如何在 Qt Creator 的共享库中进行静态链接? [英] How to have static linkage in a shared library in Qt Creator?

查看:67
本文介绍了如何在 Qt Creator 的共享库中进行静态链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明了一切.我正在使用 Qt Creator,它使用 QMake 并且我想构建一个 .so 共享库文件,该文件具有静态链接的所有依赖项.像libstdc++

The question says all the thing. I am using Qt Creator, which uses QMake and I want to build a .so shared library file that has all its dependencies statically linked. Like libstdc++, and etc.

但是当我使用 CONFIG += static 时,它也会将库更改为静态库并生成一个 .a 静态文件,这是我不想要的.所以我的问题不是 this.

But when I use CONFIG += static it also changes the library to a static library and produces a .a static file, which I don't want it. So my question is not a duplicate of this.

我搜索了这里但我无法找到任何合适的东西.

I searched here but I was not able to find any suitable thing.

推荐答案

CONFIG += static 是错误的标志,如文档所述:

CONFIG += static is the wrong flag, as stated by the documentation:

目标是一个静态库(仅限 lib).正确的编译器标志将自动添加到项目中.

The target is a static library (lib only). The proper compiler flags will automatically be added to the project.

如果您想静态链接依赖项并生成共享库,则需要将标志传递给链接器,因此将 QMAKE_LFLAGS += -static 添加到您的 .pro代码>文件.

If you want to link dependencies statically, and produce a shared library, you need to pass a flag to the linker, so add QMAKE_LFLAGS += -static to your .pro file.

一个简单的测试结果是一个 16kb 的 dll 没有那个标志,一个 995kb 的 dll 带有它.另外,如果要信任dependency walker,较大的dll没有外部依赖,而较小的依赖libgcclibstdc++(它只是一个微不足道的std::cout 测试).

A simple tests results in a 16kb dll without that flag, and a 995kb dll with it. Also, if dependency walker is to be trusted, the larger dll has no external dependencies, while the smaller depend on libgcc and libstdc++ (it is just a trivial std::cout test).

很明显,您实际上并不需要静态 qt 或 qmake 构建.使用 Qt 的库存"32 位 mingw 版本进行测试.

So evidently, you don't really need a static qt or qmake build. Tested with the "stock" 32bit mingw version of Qt.

这篇关于如何在 Qt Creator 的共享库中进行静态链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆