未链接目标二进制文件的静态 OpenSSL *.a 库(使用带有 SSL 的静态编译 Qt) [英] Static OpenSSL *.a libraries not linked target binary (using Statically Compiled Qt with SSL)

查看:79
本文介绍了未链接目标二进制文件的静态 OpenSSL *.a 库(使用带有 SSL 的静态编译 Qt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

马上就对问题的长度感到抱歉,但这是由于我提供的所有其他详细信息,我希望可以帮助更快地解决问题

我想达到什么目的?

我需要创建一个支持 SSL 的便携式(多合一)应用程序.

I need to create a portable (all-in-one) application, with SSL support.

有什么问题?

所以我面临的核心问题是在我的二进制/便携式应用程序中包含 SSL 支持.

So the core problem I am facing is getting SSL support included into my binary/portable app.

应用的MCVE很简单:

项目 .pro 文件

QT -= gui
QT += network

CONFIG += c++11 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp

INSTALLS += target

项目 main.cpp

#include <QCoreApplication>
#include <QSslSocket>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << "Is SSL Enabled? " << QSslSocket::supportsSsl();
    qDebug() << "SSL Library Build Version (Qt compiled against): " << QSslSocket::sslLibraryBuildVersionString();
    qDebug() << "SSL Library Version String (available locally): " << QSslSocket::sslLibraryVersionString();

    return a.exec();
}

<小时>

  • 在我的 DEV 机器上输出:

  •     Is SSL Enabled?  true
        SSL Library Build Version (Qt compiled against):  "OpenSSL 1.1.1d  10 Sep 2019"
        SSL Library Version String (available locally):  "OpenSSL 1.1.1d  10 Sep 2019"
    

    开发机器信息

        C:\Users\cybex>echo %PATH%
        C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin
    
    

        C:\Users\cybex>openssl
        WARNING: can't open config file: /z/extlib/_openssl_/ssl/openssl.cnf
        OpenSSL> version
        OpenSSL 1.0.2g  1 Mar 2016
    

    <小时>

    • 在全新的 Windows 10 x86 机器上运行相同的二进制文件会导致:

    •     Is SSL Enabled?  false
          SSL Library Build Version (Qt compiled against):  "OpenSSL 1.1.1d  10 Sep 2019"
          SSL Library Version String (available locally):  ""
      

      测试机信息(完全全新安装 - Windows 10 x86)

      Info of Test Machine (Completely fresh install - Windows 10 x86)

          C:\Users\cybex>echo %PATH%
          C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
      

          C:\Users\cybex>openssl
          'openssl' is not recognized as an internal or external command,
          operable program or batch file.
      

      <小时>

      • 在全新的 Windows 7 x64 机器上运行相同的二进制文件会导致:

      •     Is SSL Enabled?  false
            SSL Library Build Version (Qt compiled against):  "OpenSSL 1.1.1d  10 Sep 2019"
            SSL Library Version String (available locally):  ""
        

        测试机信息(安装驱动的Windows 7 x64笔记本电脑)

        Info of Test Machine (Windows 7 x64 laptop with drivers installed)

            C:\Users\Home>echo %PATH%
            C:\Program Files (x86)\OpenSSL\libs;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\OpenSSL\libs;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\
        

            C:\Users\Home>openssl
            'openssl' is not recognized as an internal or external command, operable program or batch file.
        

        通过查看以上结果,我得出结论,安装 OpenSSL 可以解决问题.很好,但我需要将它包含在我的便携式应用程序中.

        By look at the above results, I conclude that installing OpenSSL solves the problem. Good, but I want need to have it included in my portable app.

        为了实现这一点,我需要

        In achieving this, I am required to

        1. 使用 OpenSSL 支持静态编译 Qt

        我是在这个脚本的帮助下完成的,该脚本改编自ps1 powershell脚本此处在 Qt 的 wiki 中找到.我添加了:

        I have done this with the help of this script adapted from ps1 powershell script found here on Qt's wiki. I made additions for:

        • OpenSSL 主页 $OPENSSL_HOME

        线程数 $threads

        要使用的架构类型 $arch.

        Qt 编译细节 &OpenSSL 信息

        • 编译器:mingw32 位于 C:\Qt\Qt5.13.1\Tools\mingw730_32\bin

        mkspec:win32-g++(如果这有区别的话).

        mkspec: win32-g++ (if that makes any difference).

        OpenSSL 版本(32 位):1.1.1d

        OpenSSL version (32 bit): 1.1.1d

        配置如下:

        cmd /C "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir `
                -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -sql-sqlite -ssl -openssl -I $($OPENSSL_HOME)\include -L$($OPENSSL_HOME)\lib\MinGW`
                -opensource -confirm-license `
                -make libs -nomake tools -nomake examples -nomake tests -v"
        cmd /C "mingw32-make -k -j$($threads)"
        

        注意 1:

        我使用的是 -openssl 而不是 -openssl-linked.我已经尝试使用 -openssl-openssl-linked 构建 Qt 的几种变体.-openssl-linked 永远无法成功构建,参见 这篇文章 我已经说明了原因.

        I am using -openssl and not -openssl-linked. I have tried several variations of builing Qt with both -openssl and -openssl-linked. -openssl-linked could never successfully build, see this post I made as to the reason why.

        注意 2:

        我使用的唯一成功的静态 Qt 编译是启用了 -ssl -openssl 配置标志

        OpenSSL 安装(仅在 DEV 机器上)位于

        OpenSSL installation (on DEV machine only) is at

        `$OPENSSL_HOME = "C:\OpenSSL-Win32"` 
        

        我在哪里使用静态编译的 MinGW 库用于 OpenSSL,在

        where I am using the statically compiled MinGW libraries for OpenSSL, found in

        `$OPENSSL_HOME = "C:\OpenSSL-Win32\lib\MinGW",`
        

        C:\OpenSSL-Win32\lib\MinGW的文件内容为:

        Directory: C:\OpenSSL-Win32\lib\MinGW
        
        Mode                LastWriteTime         Length Name
        ----                -------------         ------ ----
        -a----       2019/09/11     18:11        3347286 libcrypto.a
        -a----       2019/09/11     18:10         109020 libcrypto.def
        -a----       2019/09/11     18:11         385126 libssl.a
        -a----       2019/09/11     18:10          14033 libssl.def
        

        1. 将链接项目 .pro 添加到 OpenSSL 库

        我使用 2 种方法将 OpenSSL 库添加到 .pro 文件中(库是静态编译的 MinGW OpenSSL 库,位于 C:\OpenSSL-Win32\lib\MinGW)

        I added the OpenSSL libraries to the .pro file using 2 methods(libraries are the statically compiled MinGW OpenSSL libraries found at C:\OpenSSL-Win32\lib\MinGW)

        QT -= gui
        QT += network
        
        # OpenSSL static .a libraries
        INCLUDEPATH += "C:\OpenSSL-Win32\include"
        LIBS += -L"C:\OpenSSL-Win32\lib\MinGW\libssl.a"
        LIBS += -L"C:\OpenSSL-Win32\lib\MinGW\libcrypto.a"
        
        CONFIG += c++11 console
        CONFIG -= app_bundle
        
        SOURCES += \
                main.cpp
        
        INSTALLS += target
        

        注意 3

        带有和不带有上述链接库的二进制大小保持不变

        上面添加了库的二进制文件(在我的开发机器上)的 LDD 输出是:

        The LDD output of the binary (on my dev machine) with libraries added above is:

        Start-Process -PassThru .\SSL-Test.exe | Get-Process -Module
        
           Size(K) ModuleName                                         FileName
           ------- ----------                                         --------
              6280 SSL-Test.exe                                       C:\Users\cybex\QtProjects\build-SSL-Test-Desktop_Qt_Op...
              1512 ntdll.dll                                          C:\Windows\SYSTEM32\ntdll.dll
               596 KERNEL32.DLL                                       C:\Windows\system32\KERNEL32.DLL
              1500 KERNELBASE.dll                                     C:\Windows\system32\KERNELBASE.dll
        

        上面添加了库的二进制文件(在我的 Windows 10 x86 测试机器上)的 LDD 输出是:

        The LDD output of the binary (on my Windows 10 x86 test machine) with libraries added above is:

        Start-Process -PassThru .\SSL-Test.exe | Get-Process -Module
        
           Size(K) ModuleName                                         FileName
           ------- ----------                                         --------
              6280 SSL-Test.exe                                       C:\Users\cybex\Desktop\SSL-Test.exe
              1512 ntdll.dll                                          C:\Windows\SYSTEM32\ntdll.dll
               596 KERNEL32.DLL                                       C:\Windows\system32\KERNEL32.DLL
              1500 KERNELBASE.dll                                     C:\Windows\system32\KERNELBASE.dll
        

        <小时>

        Qt 库导入到 .pro 文件中

        Import using (External Library > Import .a file> Static & Windows only带有 no debug 后缀的选项)


        Qt Library Import into .pro file

        Import using (External Library > Import .a file> Static & Windows only options with no debug suffix)

        QT -= gui
        QT += network
        
        CONFIG += c++11 console
        CONFIG -= app_bundle
        
        SOURCES += \
                main.cpp
        
        INSTALLS += target
        
        win32: LIBS += -L$$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/ -lssl
        
        INCLUDEPATH += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW
        DEPENDPATH += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW
        
        win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/ssl.lib
        else:win32-g++: PRE_TARGETDEPS += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/libssl.a
        
        win32: LIBS += -L$$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/ -lcrypto
        
        INCLUDEPATH += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW
        DEPENDPATH += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW
        
        win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/crypto.lib
        else:win32-g++: PRE_TARGETDEPS += $$PWD/../../../../OpenSSL/OpenSSL-Win32/lib/MinGW/libcrypto.a
        

        使用自动二进制导入,上面添加了库的二进制文件(在我的开发机器上)的 LDD 输出是:

        Using the automatic binary import, the LDD output of the binary (on my dev machine) with libraries added above is:

        Start-Process -PassThru .\SSL-Test.exe | Get-Process -Module
        
           Size(K) ModuleName                                         FileName
           ------- ----------                                         --------
              6280 SSL-Test.exe                                       C:\Users\cybex\QtProjects\build-SSL-Test-Desktop_Qt_Op...
              1512 ntdll.dll                                          C:\Windows\SYSTEM32\ntdll.dll
               596 KERNEL32.DLL                                       C:\Windows\system32\KERNEL32.DLL
              1500 KERNELBASE.dll                                     C:\Windows\system32\KERNELBASE.dll
        

        并且使用自动库导入,上面添加了库的二进制文件(在我的 Windows 10 x86 测试机器上)的 LDD 输出是:

        And using the automatic library import, the LDD output of the binary (on my Windows 10 x86 test machine) with libraries added above is:

        Start-Process -PassThru .\SSL-Test.exe | Get-Process -Module
        
           Size(K) ModuleName                                         FileName
           ------- ----------                                         --------
              6280 SSL-Test.exe                                       C:\Users\cybex\Desktop\SSL-Test.exe
              1512 ntdll.dll                                          C:\Windows\SYSTEM32\ntdll.dll
               596 KERNEL32.DLL                                       C:\Windows\system32\KERNEL32.DLL
              1500 KERNELBASE.dll                                     C:\Windows\system32\KERNELBASE.dll
        

        开发机器的应用程序输出是SSL enabled,正如我之前在我的帖子中提到的(对于手动和自动库条目):

        The application output for the dev machine is SSL enabled as mentioned previously in my post (for both manual and automatic library entry):

        Is SSL Enabled?  true
        SSL Library Build Version (Qt compiled against):  "OpenSSL 1.1.1d  10 Sep 2019"
        SSL Library Version String (available locally):  "OpenSSL 1.1.1d  10 Sep 2019"
        

        测试机器(未安装 OpenSSL)的应用程序输出也与之前相同(手动和自动库输入):

        The application output for the test machines (w/o OpenSSL installed) is the same as previously too (for both manual and automatic library entry):

        Is SSL Enabled?  false
        SSL Library Build Version (Qt compiled against):  "OpenSSL 1.1.1d  10 Sep 2019"
        SSL Library Version String (available locally):  ""
        

        <小时>

        因此,如果 OpenSSL 库不存在,则在非开发(新/客户端)机器上请求 SSL 连接时会导致以下错误:


        So with OpenSSL library not being present, it causes the below error when requesting a SSL connection on Non-Dev (fresh / client) machines:

        QSslSocket::connectToHostEncrypted: TLS 初始化失败

        QSslSocket::connectToHostEncrypted: TLS initialization failed

        这是 OpenSSL 库未作为依赖项包含在二进制文件中的结果.所以基本上,向项目文件添加静态 OpenSSL 库不起作用,或者我在某处做错了一些事情.

        which is the result of the OpenSSL libary not being included in the binary as a dependency. So basically, adding a static OpenSSL library to the project file does not work or I am doing something incorrectly somewhere down the line.

        注意 4:为什么问题的标题不是:如何将静态库导入 Qt?

        Note 4: Why isn't the question titled: How to Import a static library into Qt?

        导入静态库很简单,并不复杂.我假设我在启用 SSL 支持的 Qt 静态编译中的某个地方犯了一个错误.

        非常感谢您提供有关解决此问题的建议.

        Advice on solving this problem would be greatly appreciated.

        我已经用 -openssl-linked 解决了这个问题.原因(或者更确切地说是修复)是没有将 OpenSSL 安装到您的机器上.相反,提取 .a &include 目录到一个单独的目录中.所以一切都是一样的,除了你有 libs &include 无需安装.

        I have resolved the problem with -openssl-linked. The cause (or fix rather) is to not install OpenSSL to your machine. Rather, extract the .a & include directries into a seperate directory. So everything is the same with exception of you having the libs & include without installing.

        然后使用标准配置(上面提到的),将 -openssl 替换为 -openssl-linked,您应该编译成功.

        Then use the standard configureation (mentioned above), replacing -openssl with -openssl-linked and you should have a successful compile.

        更新的问题

        当构建 &使用新的Linked OpenSSL Qt kit 运行我的应用程序,我收到一条消息:

        When building & running my application with the new Linked OpenSSL Qt kit, I get a message saying:

        无法继续执行代码,因为未找到 libcrypto.dll.重新安装程序可能会解决此问题.

        The code execution cannot proceed because libcrypto.dll was not found. Reinstalling the program may fix this problem.

        接着是另一个对话框

        无法继续执行代码,因为找不到 libssl.dll.重新安装程序可能会解决此问题

        The code execution cannot proceed because libssl.dll was not found. Reinstalling the program may fix this problem

        LDD 要求:

        Start-Process -PassThru .\MyAwesomeApp.exe | Get-Process -Module
        
           Size(K) ModuleName                                         FileName
           ------- ----------                                         --------
             22708 MyAwesomeApp.exe                                     C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_OpenSSL_Linked_5_13_1_MinGW_32bit-Release\release\MyAwesomeApp.exe
              1924 ntdll.dll                                          C:\WINDOWS\SYSTEM32\ntdll.dll
               328 wow64.dll                                          C:\WINDOWS\System32\wow64.dll
               480 wow64win.dll                                       C:\WINDOWS\System32\wow64win.dll
                40 wow64cpu.dll                                       C:\WINDOWS\System32\wow64cpu.dll
        

        在使用和不使用 .pro LIBS & 的情况下测试过包含路径.两者都导致缺少 dll 的必需.

        Have tested this with and without the .pro LIBS & INCLUDEPATH. Both result in the missing dll's required.

        推荐答案

        -openssl 配置选项将 Qt 配置为使用 LoadLibrary动态加载 libopenssl> 或其他平台等效功能.所以从这一点上来说,你所有的努力都是徒劳的.您需要返回并解决 -openssl-linked 问题.

        The -openssl configuration option configures Qt to load libopenssl dynamically using LoadLibrary or another platform equivalent function. Therefore from this point on all your efforts are futile. You need to go back and solve the -openssl-linked problem.

        这篇关于未链接目标二进制文件的静态 OpenSSL *.a 库(使用带有 SSL 的静态编译 Qt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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