使用/MD开关构建静态版本的OpenSSL库 [英] Building a static version of OpenSSL library using /MD switch

查看:215
本文介绍了使用/MD开关构建静态版本的OpenSSL库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用其默认配置构建OpenSSL可为您提供该库的动态版本.根据其 Wiki 上的OpenSSL编译和安装文档,有一个名为no的配置选项-shared禁用共享对象,仅创建一个静态库.

Building OpenSSL using its default configuration gives you a dynamic version of the library. According to OpenSSL Compilation and Installation document on its Wiki, there is a configuration option named no-shared which disables shared objects and only creates a static library.

Visual C ++编译器将二进制文件(库或应用程序)链接到libcmt(d).lib(使用/MT(d)将本地CRT启动静态链接到您的代码中)或msvcrt(d).lib(静态库用于使用/MD(d)与DLL UCRT和vcruntime一起使用的本地CRT启动程序.

Visual C++ compiler links a binary (library or application) either to libcmt(d).lib (Statically links the native CRT startup into your code using /MT(d)) or msvcrt(d).lib (Static library for the native CRT startup for use with DLL UCRT and vcruntime using /MD(d)).

链接本身与CRT链接不同的二进制文件会导致冲突.不幸的是,OpenSSL没有给我们提供设置此编译器开关的选项.它始终使用/MD构建其动态版本,并始终使用/MT构建其静态版本.那么,当我们要使用/MD链接到其他静态库(例如Qt)时,如果要将其静态版本链接到应用程序,该怎么办?

Linking binaries which themselves have linked to CRT differently, results in conflicts. Unfortunately, OpenSSL did not give us the option of setting this compiler switch. It always build its dynamic version using /MD and its static version using /MT. So what should we do when we want to link a static version of it to our application when we have other static libraries which have linked using /MD such as Qt?

推荐答案

有一些可用的在线预构建二进制文件以这种方式链接,但是始终有一个很好的理由自己构建OpenSSL.最重要的原因可能是出于安全方面的考虑,或者有时您发现这些预编译的二进制文件未与所需的Visual C ++版本或所需的配置一起编译.我在网上搜索过,但是没有一种简单的方法就可以使用/MD编译器开关来构建静态版本的OpenSSL,而无需额外的复杂构建脚本.其中一些仅与旧版本的库(即1.0.x)一起使用.所以我决定分享自己的方式.

There are some pre-built binaries available online which have linked this way but there is always a good reason for building OpenSSL yourself. The most important reason might be security considerations or sometimes you find that these pre-built binaries did not compile with your desired version of Visual C++ or your desired configurations. I have searched around the Web but could not find a simple method of building a static version of OpenSSL using /MD compiler switch without extra complicated building scripts. Some of them are working just with the older version of library (i.e. 1.0.x). So I decided to share my way.

注意:在以下步骤中,请勿将任何东西放置在名称中带有空格的目录中.

NOTE: In the following steps, do not place anything in a directory with space in its name.

第1步. OpenSSL构建脚本是使用Perl编写的.因此,如果没有Perl,请先安装它.我已经使用了草莓Perl 的便携式版本.

Step 1. OpenSSL building scripts are written using Perl. So if do not have Perl, install it first. I have used the portable version of Strawberry Perl.

第2步.根据您决定为其构建OpenSSL的平台,您需要32位和/或64位版本的nasm汇编程序.您可以在此处下载副本.将所需的版本添加到您的系统路径.

Step 2. You need a 32 and/or 64 bit version of nasm assembler according to which platforms you decide to build OpenSSL for. You can download a copy here. Add the desired version to your system path.

第3步.从其网站上下载OpenSSL源的最新稳定版本. 并将其提取.从全新下载构建OpenSSL是一个好主意.因此,请保留一份副本以供以后的构建(也许使用不同的配置).

Step 3. Download the latest stable version of OpenSSL source from its web site and extract it. It's a good idea to build OpenSSL from a clean download. So keep a copy of it for later builds (perhaps with different configurations).

第4步.打开Strawberry Perl便携式外壳并转到OpenSSL源目录.

Step 4. Open the Strawberry Perl portable shell and go to OpenSSL source directory.

第5步.使用以下命令创建OpenSSL make文件:

Step 5. Create OpenSSL make files using the following command:

perl配置平台[选项] [-调试] --prefix = absolute_path --openssldir = same_path

perl Configure platform [options] [--debug] --prefix=absolute_path --openssldir=same_path

  • 平台可以是VC-WIN32,VC-WIN64A等.

  • platform can be VC-WIN32, VC-WIN64A, etc.

对我来说一些有趣的选择:

Some interesting options for me:

  • 不共享:禁用共享对象(仅创建静态库)
  • no-stdio:不要使用C头文件"stdio.h"中使用"FILE"类型的任何东西.使用此选项,您可以将最终的二进制文件链接到crypt32.lib.
  • no-sock:不建立对套接字BIO的支持.使用此选项,您可以将最终的二进制文件链接到Ws2_32.lib.
  • no-shared: Disables shared objects (only a static library is created)
  • no-stdio: Don't use anything from the C header file "stdio.h" that makes use of the "FILE" type. Using this option release you from linking your final binary to crypt32.lib.
  • no-sock: Don't build support for socket BIOs. Using this option release you from linking your final binary to Ws2_32.lib.

根据《 OpenSSL编译和安装》文档,当您想要对所有最新版本的OpenSSL(包括OpenSSL 1.0.2和1.1.0)适用"的东西时,请同时指定--prefix和--openssldir和将两者都设置在同一位置.

According to OpenSSL Compilation and Installation document, when you want something that "just works" for all recent versions of OpenSSL, including OpenSSL 1.0.2 and 1.1.0, specify both --prefix and --openssldir and set both to the same location.

步骤6..在OpenSSL源目录下,打开makefile并找到LIB_CFLAGS.根据是否编译发行版本,将/MT更改为/MD或/MDd.您可以将调试信息(pdb文件的内容)链接到库.为此,请删除/Fdossl_static并将/Zl编译器开关更改为/Z7.

Step 6. Under OpenSSL source directory, open makefile and find LIB_CFLAGS. Change /MT to /MD or /MDd according to whether you compile a release version or not. You can link debug information (the contents of pdb file) to your library. For this remove /Fdossl_static and change /Zl compiler switch to /Z7.

第7步.运行Visual Studio x86/x64本机工具命令(根据您决定为其构建OpenSSL的平台),然后转到OpenSSL源目录.然后最终运行以下命令:

Step 7. Run Visual Studio x86/x64 Native Tools Command (according to which platforms you decide to build OpenSSL for) and go to OpenSSL source directory. Then and finally run the following commands consequently:

nmake

nmake

nmake安装

享受您的时间.特别感谢我的朋友 Afshin .

Enjoy your time. With special thanks to my friend Afshin.

这篇关于使用/MD开关构建静态版本的OpenSSL库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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