在64位Windows 10上为MSVC2017安装OpenSSL [英] Install OpenSSL for MSVC2017 on 64-bit Windows 10

查看:96
本文介绍了在64位Windows 10上为MSVC2017安装OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.pro

LIBS += -LC:\Qt\Tools\OpenSSL\Win_x86\lib -llibssl
LIBS += -LC:\Qt\Tools\OpenSSL\Win_x86\lib -llibcrypto
INCLUDEPATH += C:\Qt\Tools\OpenSSL\Win_x86\include

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0

Window {
    visible: true
    width: 640
    height: 480

    Component.onCompleted: getPage(logResults)

    function logResults(results) {
        console.log("RESULTS: " + results)
    }

    function getPage(callback) {
        var xhttp = new XMLHttpRequest();
        var url = "https://www.google.com/"

        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                console.log("calling callback")
                callback(xhttp.responseText)
            }
        };
        xhttp.open("GET", url);
        xhttp.send();
    }
}

预期产量

qml: calling callback
qml: RESULTS: <html>

实际输出

qt.network.ssl: QSslSocket: cannot resolve SSL_CTX_set_ciphersuites
qt.network.ssl: QSslSocket: cannot resolve SSL_set_psk_use_session_callback
qt.network.ssl: QSslSocket: cannot call unresolved function SSL_set_psk_use_session_callback
qml: calling callback

运行MSVC2017 QML项目的Windows 10 64位操作系统

我运行C:\Qt\MaintenanceTool.exe来安装Developer and Designer Tools > OpenSSL 1.1.1d Toolkit

我尝试遵循上一教程,并且用于MSVC2017的另一种,但是解决错误或获取xhttp.responseText.发现该代码可在ubuntu 19.4中使用,所以只需要我在我的Windows机器上运行它,发现OpenSSL发生了一些奇怪的事情.通过谷歌搜索输出的错误消息找不到任何解决方案.我已经读到意外地将openSSL安装到"Windows目录"会导致错误,但是我无法真正找到问题的"Windows目录"来检查我是否这样做.

I've tried following a previous tutorial and another one for MSVC2017 but no luck in resolving the errors or getting xhttp.responseText. Found out the code works in ubuntu 19.4 so it just has to be that I'm running it on my windows machine that something funky is happening with the OpenSSL. I couldn't find any resolution by googling the outputted error messages. I've read that accidentally installing openSSL to "the windows directory" can cause errors, but I've not been able to actually locate "the windows directory" in question to check if I did.

修改

C:\Qt\Tools\OpenSSL\Win_x64\bin中,我将libcrypto-1_1-x64.dlllibssl-1_1-x64.dll复制到了项目的\debug\release文件夹中.这消除了qt.network.ssl错误,但是我仍然没有得到qml: RESULTS: <html>

From C:\Qt\Tools\OpenSSL\Win_x64\bin I copied libcrypto-1_1-x64.dll and libssl-1_1-x64.dll to my project's \debug and \release folders. This removed the qt.network.ssl errors, however I am still not getting the expected output of qml: RESULTS: <html>

推荐答案

您运行了C:\ Qt \ MaintenanceTool.exe以安装Developer和Designer. 工具> OpenSSL 1.1.1d工具包

You ran C:\Qt\MaintenanceTool.exe to install Developer and Designer Tools > OpenSSL 1.1.1d Toolkit

那也是我的建议.另一种方法是自己编译OpenSSL,或者从第三方提供商下载二进制包 .我在"C:\Program Files\OpenSSL-Win64\bin"中安装了这些软件包之一,并且使用Qt+=network的程序能够在路径环境变量中包含其路径时找到并加载这些库.问题是您将需要自己进行更新,但是Qt软件包会通过MaintenanceTool以及Qt和Qt Creator进行自动更新.因此,选择您的选择.

That is also my recommendation. The alternative is to compile OpenSSL yourself, or download a binary package from a third party provider. I have one of those packages installed at "C:\Program Files\OpenSSL-Win64\bin", and programs using Qt+=network are able to locate and load the libraries when their path is included in the PATH environment variable. The problem is that you will need to take care of the updates yourself, but the Qt packages are automatically updated with the MaintenanceTool along with Qt and Qt Creator. So pick your choice.

无论如何,即使您的路径中有另一组OpenSSL DLL,如果将库复制到可执行文件的输出目录中,也会改为加载这些库.这里需要回答两个问题:1)如何在每次需要时自动复制DLL?2)如何在运行程序时验证加载了哪些DLL?

Anyway, even if you have another set of OpenSSL DLLs in your path, if you copy the libraries to the output directory of your executable, these libraries will be loaded instead. Two questions need to be answered here: 1) how do you copy the DLLs automatically each time they are needed?, and 2) how do you verify which DLLs are loaded when you run your program?

1)您可以在项目.pro中添加以下内容:

1) You may add the following to your project .pro:

win32 {
    CONFIG += file_copies
    CONFIG(debug, debug|release) {
        openssllibs.path = $$OUT_PWD/debug
    } else {
        openssllibs.path = $$OUT_PWD/release
    }
    contains(QMAKE_TARGET.arch, x86_64) {
        openssllibs.files = C:/Qt/Tools/OpenSSL/Win_x64/bin/libcrypto-1_1-x64.dll \
                            C:/Qt/Tools/OpenSSL/Win_x64/bin/libssl-1_1-x64.dll
    } else {
        openssllibs.files = C:/Qt/Tools/OpenSSL/Win_x86/bin/libcrypto-1_1.dll \
                            C:/Qt/Tools/OpenSSL/Win_x86/bin/libssl-1_1.dll
    }
    COPIES += openssllibs
}

就是这样.现在,您的程序将始终将来自Qt/Tools的最新库复制到项目的输出目录,而不必担心您是在调试或发布模式下,还是在32/64位或其他Qt工具包中进行编译.

That is it. Now your program will always have the latest libraries from Qt/Tools copied to the output directory of your project, without worrying if you compile in debug or release mode, or 32/64 bits, or another Qt Kit.

2)运行程序,同时使用Mark Russinovich的 Process Explorer检查加载的DLL. .为此,在Process Explorer-> View-> Show下部窗格中,然后在上部窗格中选择正在运行的程序.下部窗格列出了所有已加载的DLL和来源.还有其他类似的实用程序,例如开源 Process Hacker .

2) Run your program while inspecting the loaded DLLs with Process Explorer, by Mark Russinovich. To do so, in Process Explorer->View->Show lower pane, and select your running program in the upper pane. The lower pane lists all your loaded DLLs and origins. There are other similar utilities out there, like the open source Process Hacker.

即使理解了以上所有内容,并严格按照配方进行操作,程序仍然无法打印所需的输出.请在您的qml中更改函数logResults(),如下所示:

Even understanding all of the above, and following exactly the recipe, your program still does not print the desired output. Please change the function logResults() in your qml like this:

function logResults(results) {
    console.log("RESULTS Length=", results.length);
    console.log("results.substr=", results.substr(0, 20)); 
}

您将获得以下输出:

qml: calling callback
qml: RESULTS Length= 47932
qml: results.substr= <!doctype html><html

说明:看起来console.log()在Windows中大约有32K的限制(在Linux上没有).从远程主机检索的文档要大得多,这破坏了日志记录功能.这可能是Qt中的错误(它不应像这样默默地失败).

Explanation: looks like console.log() has a limitation of about 32K in Windows (it doesn't on Linux). The document retrieved from the remote host is much larger, and this breaks the logging function. This is probably a bug in Qt (it should not fail silently like that).

将来向任何人提出的另一条建议:并非绝对必要,但是您可能想在main()函数中使用类似以下代码的内容来验证SSL可用:

Another advice for anybody coming here in the future: It is not strictly needed, but you may want to verify in your main() function that SSL is available, with something like this code:

#include <QDebug>
#include <QSslSocket>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    if (!QSslSocket::supportsSsl()) {
       qDebug() << "Sorry, OpenSSL is not supported";
       return -1;
    }
    qDebug() << "build-time OpenSSL version:" << QSslSocket::sslLibraryBuildVersionString();
    qDebug() << "run-time OpenSSL version:" << QSslSocket::sslLibraryVersionString();
    [...]
}

这篇关于在64位Windows 10上为MSVC2017安装OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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